@hakodb/client 0.6.1 → 0.6.2

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/native.ts +28 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hakodb/client",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "High-level HakoDB JavaScript/TypeScript client for Node.js and Bun",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/native.ts CHANGED
@@ -339,6 +339,12 @@ async function createBunBindings(libPath: string): Promise<NativeBindings> {
339
339
  const ffi = await import('bun:ffi');
340
340
  const { dlopen, FFIType, CString, JSCallback, toArrayBuffer } = ffi as any;
341
341
 
342
+ // Watch trampolines must outlive their native watch: the engine invokes
343
+ // them from its watch thread, and a GC'd JSCallback would leave that
344
+ // thread calling freed memory (issue #21). Closed in watchFree after
345
+ // the native join.
346
+ const watchTrampolines = new Map<unknown, { close(): void }>();
347
+
342
348
  const symbols = dlopen(libPath, {
343
349
  hk_engine_open: { args: [FFIType.cstring], returns: FFIType.ptr },
344
350
  hk_engine_is_indexes_ready: { args: [FFIType.ptr], returns: FFIType.bool },
@@ -388,6 +394,11 @@ async function createBunBindings(libPath: string): Promise<NativeBindings> {
388
394
  hk_engine_restore_available: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
389
395
  hk_engine_delete_queue: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.ptr },
390
396
  hk_engine_sweep_now: { args: [FFIType.ptr], returns: FFIType.i32 },
397
+ hk_net_syncer_new: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.ptr },
398
+ hk_net_syncer_start: { args: [FFIType.ptr, FFIType.u16], returns: FFIType.i32 },
399
+ hk_net_syncer_set_discovery: { args: [FFIType.ptr, FFIType.i32], returns: FFIType.i32 },
400
+ hk_net_syncer_status: { args: [FFIType.ptr], returns: FFIType.ptr },
401
+ hk_net_syncer_free: { args: [FFIType.ptr], returns: FFIType.void },
391
402
  hk_engine_set_collection_local: { args: [FFIType.ptr, FFIType.cstring, FFIType.i32], returns: FFIType.i32 },
392
403
  hk_engine_replicate_key: { args: [FFIType.ptr, FFIType.cstring, FFIType.cstring], returns: FFIType.i32 },
393
404
  hk_engine_replicate_collection: { args: [FFIType.ptr, FFIType.cstring], returns: FFIType.i32 },
@@ -626,12 +637,26 @@ async function createBunBindings(libPath: string): Promise<NativeBindings> {
626
637
  configSetBackgroundMaintenance: (c, e) => symbols.hk_config_set_background_maintenance(c, e),
627
638
 
628
639
  engineWatch: (engine, collection, callback) => {
640
+ // The engine calls the snapshot callback from its native watch
641
+ // thread — bun requires a threadsafe trampoline for that (marshaled
642
+ // onto the JS thread; issue #21).
629
643
  const cb = new JSCallback((c: any, p: any, kind: number) => {
630
644
  callback(new CString(c).toString(), new CString(p).toString(), kind);
631
- }, { args: [FFIType.ptr, FFIType.ptr, FFIType.i32], returns: FFIType.void });
632
- return symbols.hk_engine_watch(engine, toC(collection), cb, null);
645
+ }, { args: [FFIType.ptr, FFIType.ptr, FFIType.i32], returns: FFIType.void, threadsafe: true });
646
+ const watch = symbols.hk_engine_watch(engine, toC(collection), cb, null);
647
+ watchTrampolines.set(watch, cb);
648
+ return watch;
649
+ },
650
+ watchFree: (watch) => {
651
+ // hk_watch_free joins the watch thread — no invocation can follow,
652
+ // so the trampoline is dead and can be closed + dropped.
653
+ symbols.hk_watch_free(watch);
654
+ const cb = watchTrampolines.get(watch);
655
+ if (cb) {
656
+ cb.close();
657
+ watchTrampolines.delete(watch);
658
+ }
633
659
  },
634
- watchFree: (watch) => symbols.hk_watch_free(watch),
635
660
 
636
661
  docNew: () => symbols.hk_doc_new(),
637
662
  docFree: (doc) => symbols.hk_doc_free(doc),