nostr-dev-kit / ndk

Nostr Development Kit with outbox-model support
MIT License
360 stars 97 forks source link

fetchEvent crashes when no relay connection could be established #117

Closed steliosrammos closed 6 months ago

steliosrammos commented 1 year ago

Expected Behaviour

Throw an error.

Actual Behaviour

Crashes. In the snippet below, 'Before connect' and 'Before fetch' are logged, but 'After fetch' is not. No error is logged either.

Reproducible code

import 'websocket-polyfill';
import { generatePrivateKey, getPublicKey } from 'nostr-tools';
import NDK, { NDKPrivateKeySigner } from '@nostr-dev-kit/ndk';

const privateKey = generatePrivateKey();
const hexpubkey = getPublicKey(privateKey);
const signer = new NDKPrivateKeySigner(privateKey);
const ndk = new NDK({ signer, explicitRelayUrls: ['ws://localhost:7780'] });

const main = async () => {
    console.log('Before connect');
    await ndk.connect();

    console.log('Before fetch');
    const filter = { kinds: [5], authors: [hexpubkey] };
    const result = await ndk.fetchEvent(filter, { closeOnEose: true }).catch(err => console.log(err));
    console.log('After fetch', result);
}

main();
erskingardner commented 6 months ago

I know this is super old but this behaviour is expected. If you don't pass a timeout to the connect() function then it will just wait indefinitely to get a connection.