rust-nostr/nostr

js(sdk): Crashes on a subscription with reusing a single Filter instance

jiftechnify opened this issue · 2 comments

Describe the bug
Script crashes when you subscribe to relay(s) with reusing a single Filter instance.

The stack trace:

/home/jiftechnify/projects/code_samples/nostr/rust-nostr-js-sample/node_modules/@rust-nostr/nostr-sdk/pkg/nostr_sdk_js.js:15841
    throw new Error(getStringFromWasm0(arg0, arg1));
          ^

Error: array contains a value of the wrong type
    at module.exports.__wbindgen_throw (/home/jiftechnify/projects/code_samples/nostr/rust-nostr-js-sample/node_modules/@rust-nostr/nostr-sdk/pkg/nostr_sdk_js.js:15841:11)
    at wasm://wasm/00f06292:wasm-function[5537]:0x245357
    at wasm://wasm/00f06292:wasm-function[860]:0x14ef4e
    at wasm://wasm/00f06292:wasm-function[3987]:0x2283bc
    at wasm://wasm/00f06292:wasm-function[267]:0x99834
    at wasm://wasm/00f06292:wasm-function[1033]:0x165e4b
    at wasm://wasm/00f06292:wasm-function[4844]:0x2376be
    at wasm://wasm/00f06292:wasm-function[5091]:0x23cb4b
    at __wbg_adapter_52 (/home/jiftechnify/projects/code_samples/nostr/rust-nostr-js-sample/node_modules/@rust-nostr/nostr-sdk/pkg/nostr_sdk_js.js:248:10)
    at real (/home/jiftechnify/projects/code_samples/nostr/rust-nostr-js-sample/node_modules/@rust-nostr/nostr-sdk/pkg/nostr_sdk_js.js:173:20)

To Reproduce

import {
  ClientBuilder,
  Filter,
  FilterOptions,
  loadWasmAsync,
  SubscribeAutoCloseOptions,
} from "@rust-nostr/nostr-sdk";

await loadWasmAsync();

const client = new ClientBuilder().build();
await client.addRelays([
  "wss://nos.lol",
  "wss://relay.nostr.band"
]);
await client.connect();

const filter = new Filter().kind(1);

console.log("sub to nos.lol");
await client.subscribeTo(
  ["wss://nos.lol"],
  [filter],
);

console.log("sub to relay.nostr.band");
await client.subscribeTo(
  ["wss://relay.nostr.band"],
  [filter], // reusing the filter here
);

Expected behavior
No crash.

Build environment

  • Library: nostr-sdk
  • Language: JavaScript
  • Language version: Node.js v22.8.0
  • Tag/commit: v0.34.0
  • OS+version: Arch Linux

Unfortunately it's an issue of wasm-bindgen: when an custom object is used in a list (Vec<T>) it's moved so can't be re-used.

Until this issue is solved on their side, I can temporary add a clone method on some objects (like Filter).

So would be:

const filter = new Filter().kind(1);

console.log("sub to nos.lol");
await client.subscribeTo(
  ["wss://nos.lol"],
  [filter.clone()], // Clone here
);

console.log("sub to relay.nostr.band");
await client.subscribeTo(
  ["wss://relay.nostr.band"],
  [filter], // Filter consumed here
);

Fair enough. Thank you for looking into the issue!