mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f992c9c967 | ||
|
|
dbf625d6ac | ||
|
|
8622bd11dd | ||
|
|
0970eee70f | ||
|
|
086f8830e3 | ||
|
|
e48d722227 | ||
|
|
0d77013aab | ||
|
|
4c415280aa | ||
|
|
4188aaf7c8 | ||
|
|
673f4abab8 | ||
|
|
bcefaa0757 | ||
|
|
649af36a86 | ||
|
|
96a6f7af87 | ||
|
|
a4c713efcb | ||
|
|
9d345a8f01 | ||
|
|
c362212778 | ||
|
|
a8938a3a0f | ||
|
|
a21329da3f | ||
|
|
63f4a49a69 | ||
|
|
27749d91b8 | ||
|
|
9530849f0a | ||
|
|
b8aa75b6e1 | ||
|
|
344762820c | ||
|
|
f43d23d344 | ||
|
|
bf55ad6b5a | ||
|
|
04a46b815c | ||
|
|
165ff44dff | ||
|
|
7bfd23af3c | ||
|
|
3d93ec8446 |
1
.github/workflows/npm-publish.yml
vendored
1
.github/workflows/npm-publish.yml
vendored
@@ -16,6 +16,7 @@ jobs:
|
||||
- run: just install-dependencies
|
||||
- run: just build
|
||||
- run: just test
|
||||
- run: just emit-types
|
||||
- uses: JS-DevTools/npm-publish@v1
|
||||
with:
|
||||
token: ${{ secrets.NPM_TOKEN }}
|
||||
|
||||
35
README.md
35
README.md
@@ -10,7 +10,6 @@ Only depends on _@scure_ and _@noble_ packages.
|
||||
npm install nostr-tools # or yarn add nostr-tools
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
### Generating a private key and a public key
|
||||
@@ -120,7 +119,7 @@ let event = await relay.get({
|
||||
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
||||
})
|
||||
|
||||
await relay.close()
|
||||
relay.close()
|
||||
```
|
||||
|
||||
To use this on Node.js you first must install `websocket-polyfill` and import it:
|
||||
@@ -138,25 +137,27 @@ const pool = new SimplePool()
|
||||
|
||||
let relays = ['wss://relay.example.com', 'wss://relay.example2.com']
|
||||
|
||||
let relay = await pool.ensureRelay('wss://relay.example3.com')
|
||||
let sub = pool.sub(
|
||||
[...relays, 'wss://relay.example3.com'],
|
||||
[
|
||||
{
|
||||
authors: [
|
||||
'32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'
|
||||
]
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
let subs = pool.sub([...relays, relay], {
|
||||
authors: ['32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
||||
sub.on('event', event => {
|
||||
// this will only be called once the first time the event is received
|
||||
// ...
|
||||
})
|
||||
|
||||
subs.forEach(sub =>
|
||||
sub.on('event', event => {
|
||||
// this will only be called once the first time the event is received
|
||||
// ...
|
||||
})
|
||||
)
|
||||
|
||||
let pubs = pool.publish(relays, newEvent)
|
||||
pubs.forEach(pub =>
|
||||
pub.on('ok', () => {
|
||||
// ...
|
||||
})
|
||||
)
|
||||
pubs.on('ok', () => {
|
||||
// this may be called multiple times, once for every relay that accepts the event
|
||||
// ...
|
||||
})
|
||||
|
||||
let events = await pool.list(relays, [{kinds: [0, 1]}])
|
||||
let event = await pool.get(relays, {
|
||||
|
||||
10
build.js
10
build.js
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const fs = require('fs')
|
||||
const esbuild = require('esbuild')
|
||||
|
||||
let common = {
|
||||
@@ -11,11 +12,16 @@ let common = {
|
||||
esbuild
|
||||
.build({
|
||||
...common,
|
||||
outfile: 'lib/nostr.esm.js',
|
||||
outfile: 'lib/esm/nostr.mjs',
|
||||
format: 'esm',
|
||||
packages: 'external'
|
||||
})
|
||||
.then(() => console.log('esm build success.'))
|
||||
.then(() => {
|
||||
const packageJson = JSON.stringify({type: 'module'})
|
||||
fs.writeFileSync(`${__dirname}/lib/esm/package.json`, packageJson, 'utf8')
|
||||
|
||||
console.log('esm build success.')
|
||||
})
|
||||
|
||||
esbuild
|
||||
.build({
|
||||
|
||||
1
event.ts
1
event.ts
@@ -80,6 +80,7 @@ export function getEventHash(event: UnsignedEvent): string {
|
||||
|
||||
export function validateEvent(event: UnsignedEvent): boolean {
|
||||
if (typeof event !== 'object') return false
|
||||
if (typeof event.kind !== 'number') return false
|
||||
if (typeof event.content !== 'string') return false
|
||||
if (typeof event.created_at !== 'number') return false
|
||||
if (typeof event.pubkey !== 'string') return false
|
||||
|
||||
11
justfile
11
justfile
@@ -4,13 +4,20 @@ install-dependencies:
|
||||
yarn --ignore-engines
|
||||
|
||||
build:
|
||||
rm -rf lib
|
||||
node build.js
|
||||
|
||||
test: build
|
||||
jest
|
||||
|
||||
testOnly file: build
|
||||
test-only file: build
|
||||
jest {{file}}
|
||||
|
||||
publish: build
|
||||
emit-types:
|
||||
tsc # see tsconfig.json
|
||||
|
||||
publish: build emit-types
|
||||
npm publish
|
||||
|
||||
format:
|
||||
prettier --plugin-search-dir . --write .
|
||||
|
||||
140
magic.ts
140
magic.ts
@@ -1,140 +0,0 @@
|
||||
import {Relay, relayInit} from './relay'
|
||||
import {Event} from './event'
|
||||
import {normalizeURL} from './utils'
|
||||
|
||||
export default function (
|
||||
writeableRelays: string[],
|
||||
fallbackRelays: string[],
|
||||
safeRelays: string[]
|
||||
) {
|
||||
return new MagicPool(fallbackRelays, writeableRelays, safeRelays)
|
||||
}
|
||||
|
||||
class MagicPool {
|
||||
private _conn: {[url: string]: Relay}
|
||||
private _fallback: {[url: string]: Relay}
|
||||
private _write: {[url: string]: Relay}
|
||||
private _safe: {[url: string]: Relay}
|
||||
|
||||
private _profileRelays: {[pubkey: string]: RelayTableScore}
|
||||
private _tempCache: {[id: string]: Event}
|
||||
|
||||
constructor(
|
||||
fallbackRelays: string[],
|
||||
writeableRelays: string[],
|
||||
safeRelays: string[] = [
|
||||
'wss://eden.nostr.land',
|
||||
'wss://nostr.milou.lol',
|
||||
'wss://relay.minds.com/nostr/v1/ws'
|
||||
]
|
||||
) {
|
||||
this._conn = {}
|
||||
this._write = {}
|
||||
this._fallback = {}
|
||||
this._profileRelays = {}
|
||||
this._tempCache = {}
|
||||
|
||||
const hasEventId = (id: string): boolean => id in this._tempCache
|
||||
const init = (url: string) => {
|
||||
this._conn[normalizeURL(url)] = relayInit(normalizeURL(url), hasEventId)
|
||||
}
|
||||
|
||||
fallbackRelays.forEach(init)
|
||||
writeableRelays.forEach(init)
|
||||
safeRelays.forEach(init)
|
||||
|
||||
this._write = Object.fromEntries(
|
||||
writeableRelays.map(url => [
|
||||
normalizeURL(url),
|
||||
this._conn[normalizeURL(url)]
|
||||
])
|
||||
)
|
||||
this._fallback = Object.fromEntries(
|
||||
fallbackRelays.map(url => [
|
||||
normalizeURL(url),
|
||||
this._conn[normalizeURL(url)]
|
||||
])
|
||||
)
|
||||
this._safe = Object.fromEntries(
|
||||
safeRelays.map(url => [normalizeURL(url), this._conn[normalizeURL(url)]])
|
||||
)
|
||||
}
|
||||
|
||||
publish(event: Event) {
|
||||
return Promise.all(
|
||||
Object.entries(this._write).map(
|
||||
([url, relay]) =>
|
||||
new Promise(async resolve => {
|
||||
await relay.connect()
|
||||
let pub = relay.publish(event)
|
||||
let to = setTimeout(() => {
|
||||
let end = setTimeout(() => {
|
||||
resolve({url, success: false, reason: 'timeout'})
|
||||
}, 2500)
|
||||
pub.on('seen', () => {
|
||||
clearTimeout(end)
|
||||
resolve({url, success: true, reason: 'seen'})
|
||||
})
|
||||
}, 2500)
|
||||
pub.on('ok', () => {
|
||||
clearTimeout(to)
|
||||
resolve({url, success: true, reason: 'ok'})
|
||||
})
|
||||
pub.on('failed', (reason: string) => {
|
||||
clearTimeout(to)
|
||||
resolve({url, success: false, reason})
|
||||
})
|
||||
})
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
profile(
|
||||
pubkey: string,
|
||||
onUpdate: (events: Event[]) => void
|
||||
): {
|
||||
page(n: number): void
|
||||
} {
|
||||
var relays = new Set()
|
||||
let rts = this._profileRelays[pubkey]
|
||||
if (rts) {
|
||||
relays = rts.get(3)
|
||||
}
|
||||
|
||||
let fallback = Object.values(this._fallback)
|
||||
for (let i = 0; i < fallback.length; i++) {
|
||||
if (relays.size < 3) {
|
||||
relays.add(fallback[Math.floor(Math.random() * fallback.length)])
|
||||
} else break
|
||||
}
|
||||
|
||||
// start subscription
|
||||
for (let r in relays) {
|
||||
r.
|
||||
}
|
||||
|
||||
return {
|
||||
page(n: number) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class RelayTableScore {
|
||||
seen: string[] = []
|
||||
hinted: string[] = []
|
||||
explicit: string[] = []
|
||||
|
||||
get(n: number): Set<string> {
|
||||
let relays = new Set<string>()
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < 3; j++) {
|
||||
let v = [this.seen, this.explicit, this.hinted][j][i]
|
||||
if (v) {
|
||||
relays.add(v)
|
||||
if (relays.size >= n) return relays
|
||||
}
|
||||
}
|
||||
}
|
||||
return relays
|
||||
}
|
||||
}
|
||||
11
nip05.ts
11
nip05.ts
@@ -38,9 +38,14 @@ export async function queryProfile(
|
||||
|
||||
if (!name.match(/^[A-Za-z0-9-_]+$/)) return null
|
||||
|
||||
let res = await (
|
||||
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
||||
).json()
|
||||
let res
|
||||
try {
|
||||
res = await (
|
||||
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
||||
).json()
|
||||
} catch (err) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (!res?.names?.[name]) return null
|
||||
|
||||
|
||||
@@ -4,12 +4,16 @@ const {nip06} = require('./lib/nostr.cjs')
|
||||
test('generate private key from a mnemonic', async () => {
|
||||
const mnemonic = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong'
|
||||
const privateKey = nip06.privateKeyFromSeedWords(mnemonic)
|
||||
expect(privateKey).toEqual('c26cf31d8ba425b555ca27d00ca71b5008004f2f662470f8c8131822ec129fe2')
|
||||
expect(privateKey).toEqual(
|
||||
'c26cf31d8ba425b555ca27d00ca71b5008004f2f662470f8c8131822ec129fe2'
|
||||
)
|
||||
})
|
||||
|
||||
test('generate private key from a mnemonic and passphrase', async () => {
|
||||
const mnemonic = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong'
|
||||
const passphrase = '123'
|
||||
const privateKey = nip06.privateKeyFromSeedWords(mnemonic, passphrase)
|
||||
expect(privateKey).toEqual('55a22b8203273d0aaf24c22c8fbe99608e70c524b17265641074281c8b978ae4')
|
||||
expect(privateKey).toEqual(
|
||||
'55a22b8203273d0aaf24c22c8fbe99608e70c524b17265641074281c8b978ae4'
|
||||
)
|
||||
})
|
||||
|
||||
5
nip06.ts
5
nip06.ts
@@ -7,7 +7,10 @@ import {
|
||||
} from '@scure/bip39'
|
||||
import {HDKey} from '@scure/bip32'
|
||||
|
||||
export function privateKeyFromSeedWords(mnemonic: string, passphrase?: string): string {
|
||||
export function privateKeyFromSeedWords(
|
||||
mnemonic: string,
|
||||
passphrase?: string
|
||||
): string {
|
||||
let root = HDKey.fromMasterSeed(mnemonicToSeedSync(mnemonic, passphrase))
|
||||
let privateKey = root.derive(`m/44'/1237'/0'/0/0`).privateKey
|
||||
if (!privateKey) throw new Error('could not derive private key')
|
||||
|
||||
@@ -56,3 +56,32 @@ test('encode and decode naddr', () => {
|
||||
expect(data.kind).toEqual(30023)
|
||||
expect(data.identifier).toEqual('banana')
|
||||
})
|
||||
|
||||
test('decode naddr from habla.news', () => {
|
||||
let {type, data} = nip19.decode(
|
||||
'naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5'
|
||||
)
|
||||
expect(type).toEqual('naddr')
|
||||
expect(data.pubkey).toEqual(
|
||||
'7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194'
|
||||
)
|
||||
expect(data.kind).toEqual(30023)
|
||||
expect(data.identifier).toEqual('references')
|
||||
})
|
||||
|
||||
test('decode naddr from go-nostr with different TLV ordering', () => {
|
||||
let {type, data} = nip19.decode(
|
||||
'naddr1qqrxyctwv9hxzq3q80cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsxpqqqp65wqfwwaehxw309aex2mrp0yhxummnw3ezuetcv9khqmr99ekhjer0d4skjm3wv4uxzmtsd3jjucm0d5q3vamnwvaz7tmwdaehgu3wvfskuctwvyhxxmmd0zfmwx'
|
||||
)
|
||||
|
||||
expect(type).toEqual('naddr')
|
||||
expect(data.pubkey).toEqual(
|
||||
'3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d'
|
||||
)
|
||||
expect(data.relays).toContain(
|
||||
'wss://relay.nostr.example.mydomain.example.com'
|
||||
)
|
||||
expect(data.relays).toContain('wss://nostr.banana.com')
|
||||
expect(data.kind).toEqual(30023)
|
||||
expect(data.identifier).toEqual('banana')
|
||||
})
|
||||
|
||||
6
nip19.ts
6
nip19.ts
@@ -39,7 +39,7 @@ export function decode(nip19: string): {
|
||||
type: 'nprofile',
|
||||
data: {
|
||||
pubkey: secp256k1.utils.bytesToHex(tlv[0][0]),
|
||||
relays: tlv[1].map(d => utf8Decoder.decode(d))
|
||||
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,7 @@ export function decode(nip19: string): {
|
||||
type: 'nevent',
|
||||
data: {
|
||||
id: secp256k1.utils.bytesToHex(tlv[0][0]),
|
||||
relays: tlv[1].map(d => utf8Decoder.decode(d))
|
||||
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,7 @@ export function decode(nip19: string): {
|
||||
identifier: utf8Decoder.decode(tlv[0][0]),
|
||||
pubkey: secp256k1.utils.bytesToHex(tlv[2][0]),
|
||||
kind: parseInt(secp256k1.utils.bytesToHex(tlv[3][0]), 16),
|
||||
relays: tlv[1].map(d => utf8Decoder.decode(d))
|
||||
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
14
package.json
14
package.json
@@ -1,13 +1,21 @@
|
||||
{
|
||||
"name": "nostr-tools",
|
||||
"version": "1.5.0",
|
||||
"version": "1.7.4",
|
||||
"description": "Tools for making a Nostr client.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/fiatjaf/nostr-tools.git"
|
||||
"url": "https://github.com/nbd-wtf/nostr-tools.git"
|
||||
},
|
||||
"files": [
|
||||
"./lib/**/*"
|
||||
],
|
||||
"types": "./lib/index.d.ts",
|
||||
"main": "lib/nostr.cjs.js",
|
||||
"module": "lib/nostr.esm.js",
|
||||
"module": "lib/esm/nostr.mjs",
|
||||
"exports": {
|
||||
"import": "./lib/esm/nostr.mjs",
|
||||
"require": "./lib/nostr.cjs.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@noble/hashes": "1.0.0",
|
||||
"@noble/secp256k1": "^1.7.1",
|
||||
|
||||
10
pool.test.js
10
pool.test.js
@@ -12,17 +12,17 @@ const {
|
||||
let pool = new SimplePool()
|
||||
|
||||
let relays = [
|
||||
'wss://nostr-dev.wellorder.net/',
|
||||
'wss://relay.damus.io/',
|
||||
'wss://relay.nostr.bg/',
|
||||
'wss://nostr.fmt.wiz.biz/',
|
||||
'wss://relay.nostr.band/',
|
||||
'wss://nostr.zebedee.cloud/'
|
||||
'wss://nos.lol/'
|
||||
]
|
||||
|
||||
afterAll(async () => {
|
||||
await pool.close([
|
||||
afterAll(() => {
|
||||
pool.close([
|
||||
...relays,
|
||||
'wss://nostr-relay.untethr.me',
|
||||
'wss://nostr.wine',
|
||||
'wss://offchain.pub',
|
||||
'wss://eden.nostr.land'
|
||||
])
|
||||
|
||||
61
pool.ts
61
pool.ts
@@ -8,17 +8,20 @@ export class SimplePool {
|
||||
private _conn: {[url: string]: Relay}
|
||||
private _seenOn: {[id: string]: Set<string>} = {} // a map of all events we've seen in each relay
|
||||
|
||||
constructor() {
|
||||
private eoseSubTimeout: number
|
||||
private getTimeout: number
|
||||
|
||||
constructor(options: {eoseSubTimeout?: number; getTimeout?: number} = {}) {
|
||||
this._conn = {}
|
||||
this.eoseSubTimeout = options.eoseSubTimeout || 3400
|
||||
this.getTimeout = options.getTimeout || 3400
|
||||
}
|
||||
|
||||
async close(relays: string[]): Promise<void> {
|
||||
await Promise.all(
|
||||
relays.map(async url => {
|
||||
let relay = this._conn[normalizeURL(url)]
|
||||
if (relay) await relay.close()
|
||||
})
|
||||
)
|
||||
close(relays: string[]): void {
|
||||
relays.forEach(url => {
|
||||
let relay = this._conn[normalizeURL(url)]
|
||||
if (relay) relay.close()
|
||||
})
|
||||
}
|
||||
|
||||
async ensureRelay(url: string): Promise<Relay> {
|
||||
@@ -26,7 +29,10 @@ export class SimplePool {
|
||||
const existing = this._conn[nm]
|
||||
if (existing) return existing
|
||||
|
||||
const relay = relayInit(nm)
|
||||
const relay = relayInit(nm, {
|
||||
getTimeout: this.getTimeout * 0.9,
|
||||
listTimeout: this.getTimeout * 0.9
|
||||
})
|
||||
this._conn[nm] = relay
|
||||
|
||||
await relay.connect()
|
||||
@@ -53,10 +59,16 @@ export class SimplePool {
|
||||
let eoseTimeout = setTimeout(() => {
|
||||
eoseSent = true
|
||||
for (let cb of eoseListeners.values()) cb()
|
||||
}, 2400)
|
||||
}, this.eoseSubTimeout)
|
||||
|
||||
relays.forEach(async relay => {
|
||||
let r = await this.ensureRelay(relay)
|
||||
let r
|
||||
try {
|
||||
r = await this.ensureRelay(relay)
|
||||
} catch (err) {
|
||||
handleEose()
|
||||
return
|
||||
}
|
||||
if (!r) return
|
||||
let s = r.sub(filters, modifiedOpts)
|
||||
s.on('event', (event: Event) => {
|
||||
@@ -65,14 +77,17 @@ export class SimplePool {
|
||||
})
|
||||
s.on('eose', () => {
|
||||
if (eoseSent) return
|
||||
handleEose()
|
||||
})
|
||||
subs.push(s)
|
||||
|
||||
function handleEose() {
|
||||
eosesMissing--
|
||||
if (eosesMissing === 0) {
|
||||
clearTimeout(eoseTimeout)
|
||||
for (let cb of eoseListeners.values()) cb()
|
||||
}
|
||||
})
|
||||
subs.push(s)
|
||||
}
|
||||
})
|
||||
|
||||
let greaterSub: Sub = {
|
||||
@@ -113,7 +128,7 @@ export class SimplePool {
|
||||
let timeout = setTimeout(() => {
|
||||
sub.unsub()
|
||||
resolve(null)
|
||||
}, 1500)
|
||||
}, this.getTimeout)
|
||||
sub.on('event', (event: Event) => {
|
||||
resolve(event)
|
||||
clearTimeout(timeout)
|
||||
@@ -143,13 +158,23 @@ export class SimplePool {
|
||||
})
|
||||
}
|
||||
|
||||
publish(relays: string[], event: Event): Pub[] {
|
||||
return relays.map(relay => {
|
||||
publish(relays: string[], event: Event): Pub {
|
||||
let pubs = relays.map(relay => {
|
||||
let r = this._conn[normalizeURL(relay)]
|
||||
if (!r) return badPub(relay)
|
||||
let s = r.publish(event)
|
||||
return s
|
||||
return r.publish(event)
|
||||
})
|
||||
|
||||
return {
|
||||
on(type, cb) {
|
||||
pubs.forEach((pub, i) => {
|
||||
pub.on(type, () => cb(relays[i]))
|
||||
})
|
||||
},
|
||||
off() {
|
||||
// do nothing here, FIXME
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
seenOn(id: string): string[] {
|
||||
|
||||
@@ -9,14 +9,14 @@ const {
|
||||
signEvent
|
||||
} = require('./lib/nostr.cjs')
|
||||
|
||||
let relay = relayInit('wss://nostr-dev.wellorder.net/')
|
||||
let relay = relayInit('wss://relay.damus.io/')
|
||||
|
||||
beforeAll(() => {
|
||||
relay.connect()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await relay.close()
|
||||
afterAll(() => {
|
||||
relay.close()
|
||||
})
|
||||
|
||||
test('connectivity', () => {
|
||||
|
||||
39
relay.ts
39
relay.ts
@@ -10,7 +10,7 @@ export type Relay = {
|
||||
url: string
|
||||
status: number
|
||||
connect: () => Promise<void>
|
||||
close: () => Promise<void>
|
||||
close: () => void
|
||||
sub: (filters: Filter[], opts?: SubscriptionOptions) => Sub
|
||||
list: (filters: Filter[], opts?: SubscriptionOptions) => Promise<Event[]>
|
||||
get: (filter: Filter, opts?: SubscriptionOptions) => Promise<Event | null>
|
||||
@@ -35,9 +35,16 @@ export type SubscriptionOptions = {
|
||||
alreadyHaveEvent?: null | ((id: string, relay: string) => boolean)
|
||||
}
|
||||
|
||||
export function relayInit(url: string): Relay {
|
||||
export function relayInit(
|
||||
url: string,
|
||||
options: {
|
||||
getTimeout?: number
|
||||
listTimeout?: number
|
||||
} = {}
|
||||
): Relay {
|
||||
let {listTimeout = 3000, getTimeout = 3000} = options
|
||||
|
||||
var ws: WebSocket
|
||||
var resolveClose: () => void
|
||||
var openSubs: {[id: string]: {filters: Filter[]} & SubscriptionOptions} = {}
|
||||
var listeners: {
|
||||
connect: Array<() => void>
|
||||
@@ -78,7 +85,6 @@ export function relayInit(url: string): Relay {
|
||||
}
|
||||
ws.onclose = async () => {
|
||||
listeners.disconnect.forEach(cb => cb())
|
||||
resolveClose && resolveClose()
|
||||
}
|
||||
|
||||
let incomingMessageQueue: string[] = []
|
||||
@@ -165,14 +171,23 @@ export function relayInit(url: string): Relay {
|
||||
})
|
||||
}
|
||||
|
||||
function connected() {
|
||||
return ws?.readyState === 1
|
||||
}
|
||||
|
||||
async function connect(): Promise<void> {
|
||||
if (ws?.readyState && ws.readyState === 1) return // ws already open
|
||||
if (connected()) return // ws already open
|
||||
await connectRelay()
|
||||
}
|
||||
|
||||
async function trySend(params: [string, ...any]) {
|
||||
let msg = JSON.stringify(params)
|
||||
|
||||
if (!connected()) {
|
||||
await new Promise(resolve => setTimeout(resolve, 1000))
|
||||
if (!connected()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
try {
|
||||
ws.send(msg)
|
||||
} catch (err) {
|
||||
@@ -245,7 +260,7 @@ export function relayInit(url: string): Relay {
|
||||
let timeout = setTimeout(() => {
|
||||
s.unsub()
|
||||
resolve(events)
|
||||
}, 1500)
|
||||
}, listTimeout)
|
||||
s.on('eose', () => {
|
||||
s.unsub()
|
||||
clearTimeout(timeout)
|
||||
@@ -261,7 +276,7 @@ export function relayInit(url: string): Relay {
|
||||
let timeout = setTimeout(() => {
|
||||
s.unsub()
|
||||
resolve(null)
|
||||
}, 1500)
|
||||
}, getTimeout)
|
||||
s.on('event', (event: Event) => {
|
||||
s.unsub()
|
||||
clearTimeout(timeout)
|
||||
@@ -291,16 +306,12 @@ export function relayInit(url: string): Relay {
|
||||
}
|
||||
},
|
||||
connect,
|
||||
close(): Promise<void> {
|
||||
close(): void {
|
||||
listeners = {connect: [], disconnect: [], error: [], notice: []}
|
||||
subListeners = {}
|
||||
pubListeners = {}
|
||||
|
||||
if (ws.readyState > 1) return Promise.resolve()
|
||||
ws.close()
|
||||
return new Promise<void>(resolve => {
|
||||
resolveClose = resolve
|
||||
})
|
||||
ws?.close()
|
||||
},
|
||||
get status() {
|
||||
return ws?.readyState ?? 3
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": true,
|
||||
"emitDeclarationOnly": true,
|
||||
"outDir": "dist",
|
||||
"outDir": "lib",
|
||||
"rootDir": "."
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user