@soulcraft/cortex 1.3.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/LICENSE +16 -0
- package/README.md +125 -0
- package/dist/graph/NativeGraphAdjacencyIndex.d.ts +92 -0
- package/dist/graph/NativeGraphAdjacencyIndex.js +671 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +23 -0
- package/dist/license.d.ts +18 -0
- package/dist/license.js +172 -0
- package/dist/native/NativeEmbeddingEngine.d.ts +79 -0
- package/dist/native/NativeEmbeddingEngine.js +302 -0
- package/dist/native/NativeRoaringBitmap32.d.ts +114 -0
- package/dist/native/NativeRoaringBitmap32.js +221 -0
- package/dist/native/ffi.d.ts +20 -0
- package/dist/native/ffi.js +48 -0
- package/dist/native/index.d.ts +30 -0
- package/dist/native/index.js +58 -0
- package/dist/native/napi.d.ts +21 -0
- package/dist/native/napi.js +88 -0
- package/dist/native/types.d.ts +710 -0
- package/dist/native/types.js +16 -0
- package/dist/plugin.d.ts +22 -0
- package/dist/plugin.js +115 -0
- package/dist/storage/mmapFileSystemStorage.d.ts +24 -0
- package/dist/storage/mmapFileSystemStorage.js +73 -0
- package/dist/utils/NativeMetadataIndex.d.ts +185 -0
- package/dist/utils/NativeMetadataIndex.js +1274 -0
- package/dist/utils/nativeEntityIdMapper.d.ts +84 -0
- package/dist/utils/nativeEntityIdMapper.js +134 -0
- package/native/brainy-native.darwin-arm64.node +0 -0
- package/native/brainy-native.darwin-x64.node +0 -0
- package/native/brainy-native.linux-arm64-gnu.node +0 -0
- package/native/brainy-native.linux-x64-gnu.node +0 -0
- package/native/brainy-native.win32-x64-msvc.node +0 -0
- package/native/index.d.ts +1068 -0
- package/package.json +66 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native Roaring Bitmap - Drop-in replacement for roaring-wasm
|
|
3
|
+
*
|
|
4
|
+
* Wraps the native Rust CRoaring module to provide the EXACT same API
|
|
5
|
+
* as roaring-wasm's RoaringBitmap32.
|
|
6
|
+
*
|
|
7
|
+
* CRoaring portable serialization format is binary-identical to
|
|
8
|
+
* roaring-wasm's portable format. Existing persisted indexes are
|
|
9
|
+
* readable without migration.
|
|
10
|
+
*
|
|
11
|
+
* All roaring bitmap consumers import from src/utils/roaring/index.ts —
|
|
12
|
+
* that module swaps the implementation to use this native wrapper.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* RoaringBitmap32 - native drop-in replacement for roaring-wasm.
|
|
16
|
+
*
|
|
17
|
+
* Same API surface:
|
|
18
|
+
* Instance: .size, .isEmpty, .add(u32), .delete(u32), .tryAdd(u32), .has(u32),
|
|
19
|
+
* .toArray(), .serialize('portable'), .deserialize(buf, 'portable'),
|
|
20
|
+
* .getSerializationSizeInBytes('portable'), [Symbol.iterator]
|
|
21
|
+
* Static: RoaringBitmap32.and(a, b), .or(a, b), .orMany(arr)
|
|
22
|
+
* Constructors: new RoaringBitmap32(), new RoaringBitmap32([1,2,3])
|
|
23
|
+
*/
|
|
24
|
+
export declare class RoaringBitmap32 {
|
|
25
|
+
private inner;
|
|
26
|
+
constructor(values?: Iterable<number>);
|
|
27
|
+
/** Create from an existing native instance (internal use) */
|
|
28
|
+
private static fromNative;
|
|
29
|
+
/** Number of values in the bitmap */
|
|
30
|
+
get size(): number;
|
|
31
|
+
/** Whether the bitmap is empty */
|
|
32
|
+
get isEmpty(): boolean;
|
|
33
|
+
/** Add a value to the bitmap */
|
|
34
|
+
add(value: number): void;
|
|
35
|
+
/** Remove a value from the bitmap */
|
|
36
|
+
delete(value: number): void;
|
|
37
|
+
/** Add a value, return true if newly added */
|
|
38
|
+
tryAdd(value: number): boolean;
|
|
39
|
+
/** Check if the bitmap contains a value */
|
|
40
|
+
has(value: number): boolean;
|
|
41
|
+
/** Convert to array of u32 values (sorted) */
|
|
42
|
+
toArray(): number[];
|
|
43
|
+
/**
|
|
44
|
+
* Serialize to portable format.
|
|
45
|
+
* The format parameter is accepted for roaring-wasm API compatibility
|
|
46
|
+
* but native always uses portable format (binary-identical).
|
|
47
|
+
*/
|
|
48
|
+
serialize(_format?: 'portable' | boolean): Buffer;
|
|
49
|
+
/**
|
|
50
|
+
* Deserialize from portable format buffer (instance method).
|
|
51
|
+
* Replaces the contents of this bitmap with the deserialized data.
|
|
52
|
+
* Matches roaring-wasm instance method API.
|
|
53
|
+
*/
|
|
54
|
+
deserialize(buffer: Buffer | Uint8Array, _format?: 'portable' | boolean): void;
|
|
55
|
+
/**
|
|
56
|
+
* Get serialization size in bytes.
|
|
57
|
+
* The format parameter is accepted for roaring-wasm API compatibility.
|
|
58
|
+
*/
|
|
59
|
+
getSerializationSizeInBytes(_format?: 'portable' | boolean): number;
|
|
60
|
+
/** Get minimum value (undefined if empty) */
|
|
61
|
+
minimum(): number | undefined;
|
|
62
|
+
/** Get maximum value (undefined if empty) */
|
|
63
|
+
maximum(): number | undefined;
|
|
64
|
+
/** Iterator support for for...of loops */
|
|
65
|
+
[Symbol.iterator](): IterableIterator<number>;
|
|
66
|
+
/** Deserialize from portable format buffer */
|
|
67
|
+
static deserialize(buffer: Buffer | Uint8Array, _format?: 'portable' | boolean): RoaringBitmap32;
|
|
68
|
+
/** Compute intersection (AND) of two bitmaps */
|
|
69
|
+
static and(a: RoaringBitmap32, b: RoaringBitmap32): RoaringBitmap32;
|
|
70
|
+
/** Compute union (OR) of two bitmaps */
|
|
71
|
+
static or(a: RoaringBitmap32, b: RoaringBitmap32): RoaringBitmap32;
|
|
72
|
+
/** Compute union (OR) of multiple bitmaps */
|
|
73
|
+
static orMany(bitmaps: RoaringBitmap32[]): RoaringBitmap32;
|
|
74
|
+
/** Compute difference (AND NOT) */
|
|
75
|
+
static andNot(a: RoaringBitmap32, b: RoaringBitmap32): RoaringBitmap32;
|
|
76
|
+
/** Compute symmetric difference (XOR) */
|
|
77
|
+
static xor(a: RoaringBitmap32, b: RoaringBitmap32): RoaringBitmap32;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* RoaringBitmap32Iterator - for roaring-wasm API compatibility.
|
|
81
|
+
* Uses the native toArray() under the hood.
|
|
82
|
+
*/
|
|
83
|
+
export declare class RoaringBitmap32Iterator implements IterableIterator<number> {
|
|
84
|
+
private values;
|
|
85
|
+
private index;
|
|
86
|
+
constructor(bitmap: RoaringBitmap32);
|
|
87
|
+
next(): IteratorResult<number>;
|
|
88
|
+
[Symbol.iterator](): IterableIterator<number>;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* roaringLibraryInitialize - no-op for native (ready at load time).
|
|
92
|
+
* Kept for API compatibility with roaring-wasm.
|
|
93
|
+
*/
|
|
94
|
+
export declare function roaringLibraryInitialize(): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* roaringLibraryIsReady - always true for native.
|
|
97
|
+
* Kept for API compatibility with roaring-wasm.
|
|
98
|
+
*/
|
|
99
|
+
export declare function roaringLibraryIsReady(): boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Serialization format enum - for API compatibility with roaring-wasm.
|
|
102
|
+
*/
|
|
103
|
+
export declare const SerializationFormat: {
|
|
104
|
+
readonly portable: "portable";
|
|
105
|
+
readonly croaring: "croaring";
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Deserialization format enum - for API compatibility with roaring-wasm.
|
|
109
|
+
*/
|
|
110
|
+
export declare const DeserializationFormat: {
|
|
111
|
+
readonly portable: "portable";
|
|
112
|
+
readonly croaring: "croaring";
|
|
113
|
+
};
|
|
114
|
+
//# sourceMappingURL=NativeRoaringBitmap32.d.ts.map
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native Roaring Bitmap - Drop-in replacement for roaring-wasm
|
|
3
|
+
*
|
|
4
|
+
* Wraps the native Rust CRoaring module to provide the EXACT same API
|
|
5
|
+
* as roaring-wasm's RoaringBitmap32.
|
|
6
|
+
*
|
|
7
|
+
* CRoaring portable serialization format is binary-identical to
|
|
8
|
+
* roaring-wasm's portable format. Existing persisted indexes are
|
|
9
|
+
* readable without migration.
|
|
10
|
+
*
|
|
11
|
+
* All roaring bitmap consumers import from src/utils/roaring/index.ts —
|
|
12
|
+
* that module swaps the implementation to use this native wrapper.
|
|
13
|
+
*/
|
|
14
|
+
import { loadNativeModule } from './index.js';
|
|
15
|
+
// Lazily loaded native module
|
|
16
|
+
let _native = null;
|
|
17
|
+
function getNative() {
|
|
18
|
+
if (!_native) {
|
|
19
|
+
_native = loadNativeModule();
|
|
20
|
+
}
|
|
21
|
+
return _native;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* RoaringBitmap32 - native drop-in replacement for roaring-wasm.
|
|
25
|
+
*
|
|
26
|
+
* Same API surface:
|
|
27
|
+
* Instance: .size, .isEmpty, .add(u32), .delete(u32), .tryAdd(u32), .has(u32),
|
|
28
|
+
* .toArray(), .serialize('portable'), .deserialize(buf, 'portable'),
|
|
29
|
+
* .getSerializationSizeInBytes('portable'), [Symbol.iterator]
|
|
30
|
+
* Static: RoaringBitmap32.and(a, b), .or(a, b), .orMany(arr)
|
|
31
|
+
* Constructors: new RoaringBitmap32(), new RoaringBitmap32([1,2,3])
|
|
32
|
+
*/
|
|
33
|
+
export class RoaringBitmap32 {
|
|
34
|
+
inner;
|
|
35
|
+
constructor(values) {
|
|
36
|
+
const native = getNative();
|
|
37
|
+
if (values) {
|
|
38
|
+
const arr = Array.isArray(values) ? values : Array.from(values);
|
|
39
|
+
this.inner = new native.NativeRoaringBitmap32(arr);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this.inner = new native.NativeRoaringBitmap32();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** Create from an existing native instance (internal use) */
|
|
46
|
+
static fromNative(inner) {
|
|
47
|
+
const bm = Object.create(RoaringBitmap32.prototype);
|
|
48
|
+
bm.inner = inner;
|
|
49
|
+
return bm;
|
|
50
|
+
}
|
|
51
|
+
// =========================================================================
|
|
52
|
+
// Instance properties
|
|
53
|
+
// =========================================================================
|
|
54
|
+
/** Number of values in the bitmap */
|
|
55
|
+
get size() {
|
|
56
|
+
return this.inner.size;
|
|
57
|
+
}
|
|
58
|
+
/** Whether the bitmap is empty */
|
|
59
|
+
get isEmpty() {
|
|
60
|
+
return this.inner.isEmpty;
|
|
61
|
+
}
|
|
62
|
+
// =========================================================================
|
|
63
|
+
// Instance methods
|
|
64
|
+
// =========================================================================
|
|
65
|
+
/** Add a value to the bitmap */
|
|
66
|
+
add(value) {
|
|
67
|
+
this.inner.add(value);
|
|
68
|
+
}
|
|
69
|
+
/** Remove a value from the bitmap */
|
|
70
|
+
delete(value) {
|
|
71
|
+
this.inner.delete(value);
|
|
72
|
+
}
|
|
73
|
+
/** Add a value, return true if newly added */
|
|
74
|
+
tryAdd(value) {
|
|
75
|
+
return this.inner.tryAdd(value);
|
|
76
|
+
}
|
|
77
|
+
/** Check if the bitmap contains a value */
|
|
78
|
+
has(value) {
|
|
79
|
+
return this.inner.has(value);
|
|
80
|
+
}
|
|
81
|
+
/** Convert to array of u32 values (sorted) */
|
|
82
|
+
toArray() {
|
|
83
|
+
return this.inner.toArray();
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Serialize to portable format.
|
|
87
|
+
* The format parameter is accepted for roaring-wasm API compatibility
|
|
88
|
+
* but native always uses portable format (binary-identical).
|
|
89
|
+
*/
|
|
90
|
+
serialize(_format) {
|
|
91
|
+
return this.inner.serialize();
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Deserialize from portable format buffer (instance method).
|
|
95
|
+
* Replaces the contents of this bitmap with the deserialized data.
|
|
96
|
+
* Matches roaring-wasm instance method API.
|
|
97
|
+
*/
|
|
98
|
+
deserialize(buffer, _format) {
|
|
99
|
+
const native = getNative();
|
|
100
|
+
const buf = Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
|
|
101
|
+
this.inner = native.NativeRoaringBitmap32.deserialize(buf);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Get serialization size in bytes.
|
|
105
|
+
* The format parameter is accepted for roaring-wasm API compatibility.
|
|
106
|
+
*/
|
|
107
|
+
getSerializationSizeInBytes(_format) {
|
|
108
|
+
return this.inner.serializationSizeInBytes();
|
|
109
|
+
}
|
|
110
|
+
/** Get minimum value (undefined if empty) */
|
|
111
|
+
minimum() {
|
|
112
|
+
return this.inner.minimum();
|
|
113
|
+
}
|
|
114
|
+
/** Get maximum value (undefined if empty) */
|
|
115
|
+
maximum() {
|
|
116
|
+
return this.inner.maximum();
|
|
117
|
+
}
|
|
118
|
+
/** Iterator support for for...of loops */
|
|
119
|
+
*[Symbol.iterator]() {
|
|
120
|
+
const arr = this.inner.toArray();
|
|
121
|
+
for (const v of arr) {
|
|
122
|
+
yield v;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
// =========================================================================
|
|
126
|
+
// Static methods (match roaring-wasm static API)
|
|
127
|
+
// =========================================================================
|
|
128
|
+
/** Deserialize from portable format buffer */
|
|
129
|
+
static deserialize(buffer, _format) {
|
|
130
|
+
const native = getNative();
|
|
131
|
+
const buf = Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
|
|
132
|
+
const inner = native.NativeRoaringBitmap32.deserialize(buf);
|
|
133
|
+
return RoaringBitmap32.fromNative(inner);
|
|
134
|
+
}
|
|
135
|
+
/** Compute intersection (AND) of two bitmaps */
|
|
136
|
+
static and(a, b) {
|
|
137
|
+
const native = getNative();
|
|
138
|
+
const inner = native.NativeRoaringBitmap32.and(a.inner, b.inner);
|
|
139
|
+
return RoaringBitmap32.fromNative(inner);
|
|
140
|
+
}
|
|
141
|
+
/** Compute union (OR) of two bitmaps */
|
|
142
|
+
static or(a, b) {
|
|
143
|
+
const native = getNative();
|
|
144
|
+
const inner = native.NativeRoaringBitmap32.or(a.inner, b.inner);
|
|
145
|
+
return RoaringBitmap32.fromNative(inner);
|
|
146
|
+
}
|
|
147
|
+
/** Compute union (OR) of multiple bitmaps */
|
|
148
|
+
static orMany(bitmaps) {
|
|
149
|
+
if (bitmaps.length === 0) {
|
|
150
|
+
return new RoaringBitmap32();
|
|
151
|
+
}
|
|
152
|
+
if (bitmaps.length === 1) {
|
|
153
|
+
return bitmaps[0];
|
|
154
|
+
}
|
|
155
|
+
const native = getNative();
|
|
156
|
+
const inner = native.NativeRoaringBitmap32.orMany(bitmaps.map(bm => bm.inner));
|
|
157
|
+
return RoaringBitmap32.fromNative(inner);
|
|
158
|
+
}
|
|
159
|
+
/** Compute difference (AND NOT) */
|
|
160
|
+
static andNot(a, b) {
|
|
161
|
+
const native = getNative();
|
|
162
|
+
const inner = native.NativeRoaringBitmap32.andNot(a.inner, b.inner);
|
|
163
|
+
return RoaringBitmap32.fromNative(inner);
|
|
164
|
+
}
|
|
165
|
+
/** Compute symmetric difference (XOR) */
|
|
166
|
+
static xor(a, b) {
|
|
167
|
+
const native = getNative();
|
|
168
|
+
const inner = native.NativeRoaringBitmap32.xor(a.inner, b.inner);
|
|
169
|
+
return RoaringBitmap32.fromNative(inner);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* RoaringBitmap32Iterator - for roaring-wasm API compatibility.
|
|
174
|
+
* Uses the native toArray() under the hood.
|
|
175
|
+
*/
|
|
176
|
+
export class RoaringBitmap32Iterator {
|
|
177
|
+
values;
|
|
178
|
+
index = 0;
|
|
179
|
+
constructor(bitmap) {
|
|
180
|
+
this.values = bitmap.toArray();
|
|
181
|
+
}
|
|
182
|
+
next() {
|
|
183
|
+
if (this.index < this.values.length) {
|
|
184
|
+
return { value: this.values[this.index++], done: false };
|
|
185
|
+
}
|
|
186
|
+
return { value: undefined, done: true };
|
|
187
|
+
}
|
|
188
|
+
[Symbol.iterator]() {
|
|
189
|
+
return this;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* roaringLibraryInitialize - no-op for native (ready at load time).
|
|
194
|
+
* Kept for API compatibility with roaring-wasm.
|
|
195
|
+
*/
|
|
196
|
+
export async function roaringLibraryInitialize() {
|
|
197
|
+
// Native module is ready at load time - no WASM compilation needed
|
|
198
|
+
return Promise.resolve();
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* roaringLibraryIsReady - always true for native.
|
|
202
|
+
* Kept for API compatibility with roaring-wasm.
|
|
203
|
+
*/
|
|
204
|
+
export function roaringLibraryIsReady() {
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Serialization format enum - for API compatibility with roaring-wasm.
|
|
209
|
+
*/
|
|
210
|
+
export const SerializationFormat = {
|
|
211
|
+
portable: 'portable',
|
|
212
|
+
croaring: 'croaring',
|
|
213
|
+
};
|
|
214
|
+
/**
|
|
215
|
+
* Deserialization format enum - for API compatibility with roaring-wasm.
|
|
216
|
+
*/
|
|
217
|
+
export const DeserializationFormat = {
|
|
218
|
+
portable: 'portable',
|
|
219
|
+
croaring: 'croaring',
|
|
220
|
+
};
|
|
221
|
+
//# sourceMappingURL=NativeRoaringBitmap32.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bun:ffi bindings loader
|
|
3
|
+
*
|
|
4
|
+
* Loads the native module via bun:ffi + dlopen for bun --compile on Linux.
|
|
5
|
+
* The .so file is embedded in the compiled binary via static import.
|
|
6
|
+
*
|
|
7
|
+
* This is the PREFERRED production deployment path:
|
|
8
|
+
* bun build --compile my-app.ts --outfile my-app
|
|
9
|
+
*
|
|
10
|
+
* For development and other platforms, use napi.ts instead.
|
|
11
|
+
*/
|
|
12
|
+
import type { NativeBindings } from './types.js';
|
|
13
|
+
/**
|
|
14
|
+
* Load the native module via bun:ffi.
|
|
15
|
+
*
|
|
16
|
+
* Only available on Linux with Bun.
|
|
17
|
+
* Falls back to napi-rs on other platforms or if FFI loading fails.
|
|
18
|
+
*/
|
|
19
|
+
export declare function loadViaFFI(): NativeBindings;
|
|
20
|
+
//# sourceMappingURL=ffi.d.ts.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bun:ffi bindings loader
|
|
3
|
+
*
|
|
4
|
+
* Loads the native module via bun:ffi + dlopen for bun --compile on Linux.
|
|
5
|
+
* The .so file is embedded in the compiled binary via static import.
|
|
6
|
+
*
|
|
7
|
+
* This is the PREFERRED production deployment path:
|
|
8
|
+
* bun build --compile my-app.ts --outfile my-app
|
|
9
|
+
*
|
|
10
|
+
* For development and other platforms, use napi.ts instead.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Load the native module via bun:ffi.
|
|
14
|
+
*
|
|
15
|
+
* Only available on Linux with Bun.
|
|
16
|
+
* Falls back to napi-rs on other platforms or if FFI loading fails.
|
|
17
|
+
*/
|
|
18
|
+
export function loadViaFFI() {
|
|
19
|
+
// bun:ffi is only available in Bun
|
|
20
|
+
if (typeof Bun === 'undefined') {
|
|
21
|
+
throw new Error('bun:ffi is only available in Bun runtime');
|
|
22
|
+
}
|
|
23
|
+
if (process.platform !== 'linux') {
|
|
24
|
+
throw new Error('bun:ffi native loading is only supported on Linux');
|
|
25
|
+
}
|
|
26
|
+
// Dynamic import of bun:ffi — this is a Bun built-in
|
|
27
|
+
// The .so file path is resolved at build time for bun --compile
|
|
28
|
+
// For runtime Bun, it falls back to the local build path
|
|
29
|
+
throw new Error('bun:ffi loader not yet implemented. ' +
|
|
30
|
+
'Requires native build output (libbrainy_native.so). ' +
|
|
31
|
+
'Use napi-rs loader instead.');
|
|
32
|
+
// Future implementation:
|
|
33
|
+
// import nativeLib from '../../native/target/release/libbrainy_native.so' with { type: 'file' }
|
|
34
|
+
// import { dlopen, FFIType } from 'bun:ffi'
|
|
35
|
+
//
|
|
36
|
+
// const lib = dlopen(nativeLib, {
|
|
37
|
+
// brainy_engine_create: { returns: FFIType.ptr },
|
|
38
|
+
// brainy_engine_load: { args: [...], returns: FFIType.i32 },
|
|
39
|
+
// brainy_engine_embed: { args: [...], returns: FFIType.ptr },
|
|
40
|
+
// brainy_roaring_create: { returns: FFIType.ptr },
|
|
41
|
+
// brainy_roaring_add: { args: [FFIType.ptr, FFIType.u32], returns: FFIType.void },
|
|
42
|
+
// brainy_cosine_distance: { args: [FFIType.ptr, FFIType.ptr, FFIType.u32], returns: FFIType.f64 },
|
|
43
|
+
// // ... etc
|
|
44
|
+
// })
|
|
45
|
+
//
|
|
46
|
+
// return wrapFFIAsNativeBindings(lib)
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=ffi.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native Module - Public API
|
|
3
|
+
*
|
|
4
|
+
* Auto-detects the best loading strategy:
|
|
5
|
+
* 1. Bun --compile on Linux: bun:ffi with embedded .so (fastest)
|
|
6
|
+
* 2. All platforms: napi-rs .node file (universal)
|
|
7
|
+
*
|
|
8
|
+
* Exports the unified NativeBindings interface that all consumers use.
|
|
9
|
+
*/
|
|
10
|
+
import type { NativeBindings } from './types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Load the native module using the best available strategy.
|
|
13
|
+
*
|
|
14
|
+
* Returns a cached reference on subsequent calls.
|
|
15
|
+
*
|
|
16
|
+
* Loading priority:
|
|
17
|
+
* 1. Bun --compile on Linux → bun:ffi + embedded .so
|
|
18
|
+
* 2. All platforms → napi-rs .node file
|
|
19
|
+
*/
|
|
20
|
+
export declare function loadNativeModule(): NativeBindings;
|
|
21
|
+
/**
|
|
22
|
+
* Check if the native module is available.
|
|
23
|
+
* Does not throw - returns false if loading fails.
|
|
24
|
+
*/
|
|
25
|
+
export declare function isNativeAvailable(): boolean;
|
|
26
|
+
export type { NativeBindings, NativeEmbeddingEngineInstance, NativeRoaringBitmap32Instance, EmbeddingResult, EngineStats, DeviceInfo, } from './types.js';
|
|
27
|
+
export { MODEL_CONSTANTS } from './types.js';
|
|
28
|
+
export { NativeEmbeddingEngine, nativeEmbeddingEngine, cosineSimilarity, } from './NativeEmbeddingEngine.js';
|
|
29
|
+
export { RoaringBitmap32, RoaringBitmap32Iterator, roaringLibraryInitialize, roaringLibraryIsReady, SerializationFormat, DeserializationFormat, } from './NativeRoaringBitmap32.js';
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native Module - Public API
|
|
3
|
+
*
|
|
4
|
+
* Auto-detects the best loading strategy:
|
|
5
|
+
* 1. Bun --compile on Linux: bun:ffi with embedded .so (fastest)
|
|
6
|
+
* 2. All platforms: napi-rs .node file (universal)
|
|
7
|
+
*
|
|
8
|
+
* Exports the unified NativeBindings interface that all consumers use.
|
|
9
|
+
*/
|
|
10
|
+
import { loadViaNapi } from './napi.js';
|
|
11
|
+
import { loadViaFFI } from './ffi.js';
|
|
12
|
+
// Cached module reference
|
|
13
|
+
let cachedModule = null;
|
|
14
|
+
/**
|
|
15
|
+
* Load the native module using the best available strategy.
|
|
16
|
+
*
|
|
17
|
+
* Returns a cached reference on subsequent calls.
|
|
18
|
+
*
|
|
19
|
+
* Loading priority:
|
|
20
|
+
* 1. Bun --compile on Linux → bun:ffi + embedded .so
|
|
21
|
+
* 2. All platforms → napi-rs .node file
|
|
22
|
+
*/
|
|
23
|
+
export function loadNativeModule() {
|
|
24
|
+
if (cachedModule) {
|
|
25
|
+
return cachedModule;
|
|
26
|
+
}
|
|
27
|
+
// Strategy 1: Bun --compile on Linux (bun:ffi)
|
|
28
|
+
if (process.platform === 'linux' && typeof Bun !== 'undefined') {
|
|
29
|
+
try {
|
|
30
|
+
cachedModule = loadViaFFI();
|
|
31
|
+
return cachedModule;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
// Fall through to napi-rs (Bun runtime, not compiled binary)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
// Strategy 2: All platforms (napi-rs)
|
|
38
|
+
cachedModule = loadViaNapi();
|
|
39
|
+
return cachedModule;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Check if the native module is available.
|
|
43
|
+
* Does not throw - returns false if loading fails.
|
|
44
|
+
*/
|
|
45
|
+
export function isNativeAvailable() {
|
|
46
|
+
try {
|
|
47
|
+
loadNativeModule();
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export { MODEL_CONSTANTS } from './types.js';
|
|
55
|
+
// Re-export engine and roaring wrappers
|
|
56
|
+
export { NativeEmbeddingEngine, nativeEmbeddingEngine, cosineSimilarity, } from './NativeEmbeddingEngine.js';
|
|
57
|
+
export { RoaringBitmap32, RoaringBitmap32Iterator, roaringLibraryInitialize, roaringLibraryIsReady, SerializationFormat, DeserializationFormat, } from './NativeRoaringBitmap32.js';
|
|
58
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* napi-rs bindings loader
|
|
3
|
+
*
|
|
4
|
+
* Loads the native module via napi-rs .node files.
|
|
5
|
+
* Works with: Node.js, Bun runtime, Bun --compile (all platforms).
|
|
6
|
+
*
|
|
7
|
+
* Resolution order:
|
|
8
|
+
* 1. Bundled platform binary: native/brainy-native.{platform-key}.node (shipped in npm package)
|
|
9
|
+
* 2. Local dev build: native/brainy-native.node (for development, no platform suffix)
|
|
10
|
+
* 3. Clear error with supported platforms list
|
|
11
|
+
*/
|
|
12
|
+
import type { NativeBindings } from './types.js';
|
|
13
|
+
/**
|
|
14
|
+
* Load the native module via napi-rs.
|
|
15
|
+
*
|
|
16
|
+
* Tries multiple resolution strategies:
|
|
17
|
+
* 1. Bundled platform binary (native/brainy-native.{platform-key}.node)
|
|
18
|
+
* 2. Local dev build (native/brainy-native.node)
|
|
19
|
+
*/
|
|
20
|
+
export declare function loadViaNapi(): NativeBindings;
|
|
21
|
+
//# sourceMappingURL=napi.d.ts.map
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* napi-rs bindings loader
|
|
3
|
+
*
|
|
4
|
+
* Loads the native module via napi-rs .node files.
|
|
5
|
+
* Works with: Node.js, Bun runtime, Bun --compile (all platforms).
|
|
6
|
+
*
|
|
7
|
+
* Resolution order:
|
|
8
|
+
* 1. Bundled platform binary: native/brainy-native.{platform-key}.node (shipped in npm package)
|
|
9
|
+
* 2. Local dev build: native/brainy-native.node (for development, no platform suffix)
|
|
10
|
+
* 3. Clear error with supported platforms list
|
|
11
|
+
*/
|
|
12
|
+
import { join, dirname } from 'node:path';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
let cachedBindings = null;
|
|
15
|
+
/** Platform keys matching napi-rs build output */
|
|
16
|
+
const PLATFORM_KEY_MAP = {
|
|
17
|
+
'linux-x64': 'linux-x64-gnu',
|
|
18
|
+
'linux-arm64': 'linux-arm64-gnu',
|
|
19
|
+
'darwin-arm64': 'darwin-arm64',
|
|
20
|
+
'darwin-x64': 'darwin-x64',
|
|
21
|
+
'win32-x64': 'win32-x64-msvc'
|
|
22
|
+
};
|
|
23
|
+
const SUPPORTED_PLATFORMS = Object.keys(PLATFORM_KEY_MAP);
|
|
24
|
+
/**
|
|
25
|
+
* Load the native module via napi-rs.
|
|
26
|
+
*
|
|
27
|
+
* Tries multiple resolution strategies:
|
|
28
|
+
* 1. Bundled platform binary (native/brainy-native.{platform-key}.node)
|
|
29
|
+
* 2. Local dev build (native/brainy-native.node)
|
|
30
|
+
*/
|
|
31
|
+
export function loadViaNapi() {
|
|
32
|
+
if (cachedBindings) {
|
|
33
|
+
return cachedBindings;
|
|
34
|
+
}
|
|
35
|
+
const platform = process.platform;
|
|
36
|
+
const arch = process.arch;
|
|
37
|
+
const key = `${platform}-${arch}`;
|
|
38
|
+
const platformKey = PLATFORM_KEY_MAP[key];
|
|
39
|
+
if (!platformKey) {
|
|
40
|
+
throw new Error(`Unsupported platform: ${key}. ` +
|
|
41
|
+
`Brainy native module supports: ${SUPPORTED_PLATFORMS.join(', ')}`);
|
|
42
|
+
}
|
|
43
|
+
const { createRequire } = loadModule('node:module');
|
|
44
|
+
const require = createRequire(import.meta.url);
|
|
45
|
+
// Resolve native/ directory relative to package root
|
|
46
|
+
// This file lives at dist/native/napi.js → package root is ../../
|
|
47
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
48
|
+
const nativeDir = join(__dirname, '..', '..', 'native');
|
|
49
|
+
// Strategy 1: Bundled platform binary (shipped in npm package)
|
|
50
|
+
try {
|
|
51
|
+
const binaryPath = join(nativeDir, `brainy-native.${platformKey}.node`);
|
|
52
|
+
const bindings = require(binaryPath);
|
|
53
|
+
cachedBindings = bindings;
|
|
54
|
+
return bindings;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// Fall through to strategy 2
|
|
58
|
+
}
|
|
59
|
+
// Strategy 2: Local dev build (no platform suffix)
|
|
60
|
+
try {
|
|
61
|
+
const devPath = join(nativeDir, 'brainy-native.node');
|
|
62
|
+
const bindings = require(devPath);
|
|
63
|
+
cachedBindings = bindings;
|
|
64
|
+
return bindings;
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// Fall through to error
|
|
68
|
+
}
|
|
69
|
+
throw new Error(`Failed to load Brainy native module for ${key}.\n` +
|
|
70
|
+
`Looked for:\n` +
|
|
71
|
+
` - native/brainy-native.${platformKey}.node (bundled binary)\n` +
|
|
72
|
+
` - native/brainy-native.node (local dev build)\n` +
|
|
73
|
+
`Build locally: cd native && npm run build`);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Synchronously load a Node.js built-in module.
|
|
77
|
+
* Workaround for ESM dynamic import restrictions.
|
|
78
|
+
*/
|
|
79
|
+
function loadModule(moduleName) {
|
|
80
|
+
if (typeof require === 'function') {
|
|
81
|
+
return require(moduleName);
|
|
82
|
+
}
|
|
83
|
+
if (typeof process !== 'undefined' && process.getBuiltinModule) {
|
|
84
|
+
return process.getBuiltinModule(moduleName);
|
|
85
|
+
}
|
|
86
|
+
throw new Error(`Cannot synchronously load module: ${moduleName}`);
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=napi.js.map
|