Compare commits

..

5 Commits

Author SHA1 Message Date
fiatjaf
39ea47660d use a different relay for tests. 2022-12-25 16:01:31 -03:00
Tristan
8071e2f4fa Make opts arg optional for sub method
In the README and the code, it looks like the second argument for the relay's `sub` method is optional:

```typescript
let sub = relay.sub([
  {
    ids: ['d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027']
  }
])
```

In the type definitions it's required however, which leads to an error in editors. Let's mark it as optional in the type definitions too! 👍
2022-12-25 14:32:20 -03:00
Tristan
cc2250da1f Add missing "error" event to on and off type definitions 2022-12-25 14:31:38 -03:00
rkfg
c37d10bb9d Fix resolveClose 2022-12-24 20:41:49 -03:00
rkfg
97e28fdf9a Fix connect/close return types and race condition 2022-12-24 18:49:16 -03:00
3 changed files with 13 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "nostr-tools",
"version": "1.0.0",
"version": "1.0.1",
"description": "Tools for making a Nostr client.",
"repository": {
"type": "git",

View File

@@ -9,7 +9,7 @@ const {
signEvent
} = require('./lib/nostr.cjs')
let relay = relayInit('wss://nostr-pub.semisol.dev/')
let relay = relayInit('wss://nostr-dev.wellorder.net/')
beforeAll(() => {
relay.connect()

View File

@@ -3,15 +3,17 @@
import {Event, verifySignature, validateEvent} from './event'
import {Filter, matchFilters} from './filter'
type RelayEvent = 'connect' | 'disconnect' | 'error' | 'notice'
export type Relay = {
url: string
status: number
connect: () => void
close: () => void
sub: (filters: Filter[], opts: SubscriptionOptions) => Sub
connect: () => Promise<void>
close: () => Promise<void>
sub: (filters: Filter[], opts?: SubscriptionOptions) => Sub
publish: (event: Event) => Pub
on: (type: 'connect' | 'disconnect' | 'notice', cb: any) => void
off: (type: 'connect' | 'disconnect' | 'notice', cb: any) => void
on: (type: RelayEvent, cb: any) => void
off: (type: RelayEvent, cb: any) => void
}
export type Pub = {
on: (type: 'ok' | 'seen' | 'failed', cb: any) => void
@@ -73,7 +75,7 @@ export function relayInit(url: string): Relay {
}
ws.onclose = async () => {
listeners.disconnect.forEach(cb => cb())
resolveClose()
resolveClose && resolveClose()
}
ws.onmessage = async e => {
@@ -185,7 +187,7 @@ export function relayInit(url: string): Relay {
url,
sub,
on: (
type: 'connect' | 'disconnect' | 'error' | 'notice',
type: RelayEvent,
cb: any
): void => {
listeners[type].push(cb)
@@ -194,7 +196,7 @@ export function relayInit(url: string): Relay {
}
},
off: (
type: 'connect' | 'disconnect' | 'error' | 'notice',
type: RelayEvent,
cb: any
): void => {
let index = listeners[type].indexOf(cb)
@@ -258,7 +260,7 @@ export function relayInit(url: string): Relay {
connect,
close(): Promise<void> {
ws.close()
return new Promise(resolve => {
return new Promise<void>(resolve => {
resolveClose = resolve
})
},