mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f992c9c967 | ||
|
|
dbf625d6ac | ||
|
|
8622bd11dd | ||
|
|
0970eee70f | ||
|
|
086f8830e3 | ||
|
|
e48d722227 | ||
|
|
0d77013aab | ||
|
|
4c415280aa | ||
|
|
4188aaf7c8 | ||
|
|
673f4abab8 | ||
|
|
bcefaa0757 | ||
|
|
649af36a86 | ||
|
|
96a6f7af87 | ||
|
|
a4c713efcb | ||
|
|
9d345a8f01 | ||
|
|
c362212778 | ||
|
|
a8938a3a0f | ||
|
|
a21329da3f | ||
|
|
63f4a49a69 |
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 install-dependencies
|
||||||
- run: just build
|
- run: just build
|
||||||
- run: just test
|
- run: just test
|
||||||
|
- run: just emit-types
|
||||||
- uses: JS-DevTools/npm-publish@v1
|
- uses: JS-DevTools/npm-publish@v1
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.NPM_TOKEN }}
|
token: ${{ secrets.NPM_TOKEN }}
|
||||||
|
|||||||
25
README.md
25
README.md
@@ -10,7 +10,6 @@ Only depends on _@scure_ and _@noble_ packages.
|
|||||||
npm install nostr-tools # or yarn add nostr-tools
|
npm install nostr-tools # or yarn add nostr-tools
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### Generating a private key and a public key
|
### Generating a private key and a public key
|
||||||
@@ -138,11 +137,16 @@ const pool = new SimplePool()
|
|||||||
|
|
||||||
let relays = ['wss://relay.example.com', 'wss://relay.example2.com']
|
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'],
|
||||||
let sub = pool.sub([...relays, relay], [{
|
[
|
||||||
authors: ['32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
{
|
||||||
}])
|
authors: [
|
||||||
|
'32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
sub.on('event', event => {
|
sub.on('event', event => {
|
||||||
// this will only be called once the first time the event is received
|
// this will only be called once the first time the event is received
|
||||||
@@ -150,11 +154,10 @@ sub.on('event', event => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
let pubs = pool.publish(relays, newEvent)
|
let pubs = pool.publish(relays, newEvent)
|
||||||
pubs.forEach(pub =>
|
pubs.on('ok', () => {
|
||||||
pub.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 events = await pool.list(relays, [{kinds: [0, 1]}])
|
||||||
let event = await pool.get(relays, {
|
let event = await pool.get(relays, {
|
||||||
|
|||||||
2
build.js
2
build.js
@@ -17,7 +17,7 @@ esbuild
|
|||||||
packages: 'external'
|
packages: 'external'
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const packageJson = JSON.stringify({ type: 'module' })
|
const packageJson = JSON.stringify({type: 'module'})
|
||||||
fs.writeFileSync(`${__dirname}/lib/esm/package.json`, packageJson, 'utf8')
|
fs.writeFileSync(`${__dirname}/lib/esm/package.json`, packageJson, 'utf8')
|
||||||
|
|
||||||
console.log('esm build success.')
|
console.log('esm build success.')
|
||||||
|
|||||||
1
event.ts
1
event.ts
@@ -80,6 +80,7 @@ export function getEventHash(event: UnsignedEvent): string {
|
|||||||
|
|
||||||
export function validateEvent(event: UnsignedEvent): boolean {
|
export function validateEvent(event: UnsignedEvent): boolean {
|
||||||
if (typeof event !== 'object') return false
|
if (typeof event !== 'object') return false
|
||||||
|
if (typeof event.kind !== 'number') return false
|
||||||
if (typeof event.content !== 'string') return false
|
if (typeof event.content !== 'string') return false
|
||||||
if (typeof event.created_at !== 'number') return false
|
if (typeof event.created_at !== 'number') return false
|
||||||
if (typeof event.pubkey !== 'string') return false
|
if (typeof event.pubkey !== 'string') return false
|
||||||
|
|||||||
11
justfile
11
justfile
@@ -4,13 +4,20 @@ install-dependencies:
|
|||||||
yarn --ignore-engines
|
yarn --ignore-engines
|
||||||
|
|
||||||
build:
|
build:
|
||||||
|
rm -rf lib
|
||||||
node build.js
|
node build.js
|
||||||
|
|
||||||
test: build
|
test: build
|
||||||
jest
|
jest
|
||||||
|
|
||||||
testOnly file: build
|
test-only file: build
|
||||||
jest {{file}}
|
jest {{file}}
|
||||||
|
|
||||||
publish: build
|
emit-types:
|
||||||
|
tsc # see tsconfig.json
|
||||||
|
|
||||||
|
publish: build emit-types
|
||||||
npm publish
|
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
|
if (!name.match(/^[A-Za-z0-9-_]+$/)) return null
|
||||||
|
|
||||||
let res = await (
|
let res
|
||||||
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
try {
|
||||||
).json()
|
res = await (
|
||||||
|
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
||||||
|
).json()
|
||||||
|
} catch (err) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
if (!res?.names?.[name]) 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 () => {
|
test('generate private key from a mnemonic', async () => {
|
||||||
const mnemonic = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong'
|
const mnemonic = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong'
|
||||||
const privateKey = nip06.privateKeyFromSeedWords(mnemonic)
|
const privateKey = nip06.privateKeyFromSeedWords(mnemonic)
|
||||||
expect(privateKey).toEqual('c26cf31d8ba425b555ca27d00ca71b5008004f2f662470f8c8131822ec129fe2')
|
expect(privateKey).toEqual(
|
||||||
|
'c26cf31d8ba425b555ca27d00ca71b5008004f2f662470f8c8131822ec129fe2'
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('generate private key from a mnemonic and passphrase', async () => {
|
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 mnemonic = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong'
|
||||||
const passphrase = '123'
|
const passphrase = '123'
|
||||||
const privateKey = nip06.privateKeyFromSeedWords(mnemonic, passphrase)
|
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'
|
} from '@scure/bip39'
|
||||||
import {HDKey} from '@scure/bip32'
|
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 root = HDKey.fromMasterSeed(mnemonicToSeedSync(mnemonic, passphrase))
|
||||||
let privateKey = root.derive(`m/44'/1237'/0'/0/0`).privateKey
|
let privateKey = root.derive(`m/44'/1237'/0'/0/0`).privateKey
|
||||||
if (!privateKey) throw new Error('could not derive private key')
|
if (!privateKey) throw new Error('could not derive private key')
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ test('encode and decode naddr', () => {
|
|||||||
expect(data.identifier).toEqual('banana')
|
expect(data.identifier).toEqual('banana')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('encode and decode naddr from habla.news', () => {
|
test('decode naddr from habla.news', () => {
|
||||||
let {type, data} = nip19.decode(
|
let {type, data} = nip19.decode(
|
||||||
'naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5'
|
'naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5'
|
||||||
)
|
)
|
||||||
@@ -68,3 +68,20 @@ test('encode and decode naddr from habla.news', () => {
|
|||||||
expect(data.kind).toEqual(30023)
|
expect(data.kind).toEqual(30023)
|
||||||
expect(data.identifier).toEqual('references')
|
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')
|
||||||
|
})
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.6.1",
|
"version": "1.7.4",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"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",
|
"main": "lib/nostr.cjs.js",
|
||||||
"module": "lib/esm/nostr.mjs",
|
"module": "lib/esm/nostr.mjs",
|
||||||
"exports": {
|
"exports": {
|
||||||
|
|||||||
@@ -12,17 +12,17 @@ const {
|
|||||||
let pool = new SimplePool()
|
let pool = new SimplePool()
|
||||||
|
|
||||||
let relays = [
|
let relays = [
|
||||||
'wss://nostr-dev.wellorder.net/',
|
'wss://relay.damus.io/',
|
||||||
'wss://relay.nostr.bg/',
|
'wss://relay.nostr.bg/',
|
||||||
'wss://nostr.fmt.wiz.biz/',
|
'wss://nostr.fmt.wiz.biz/',
|
||||||
'wss://relay.nostr.band/',
|
'wss://relay.nostr.band/',
|
||||||
'wss://nostr.zebedee.cloud/'
|
'wss://nos.lol/'
|
||||||
]
|
]
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
pool.close([
|
pool.close([
|
||||||
...relays,
|
...relays,
|
||||||
'wss://nostr-relay.untethr.me',
|
'wss://nostr.wine',
|
||||||
'wss://offchain.pub',
|
'wss://offchain.pub',
|
||||||
'wss://eden.nostr.land'
|
'wss://eden.nostr.land'
|
||||||
])
|
])
|
||||||
|
|||||||
34
pool.ts
34
pool.ts
@@ -8,8 +8,13 @@ export class SimplePool {
|
|||||||
private _conn: {[url: string]: Relay}
|
private _conn: {[url: string]: Relay}
|
||||||
private _seenOn: {[id: string]: Set<string>} = {} // a map of all events we've seen in each 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._conn = {}
|
||||||
|
this.eoseSubTimeout = options.eoseSubTimeout || 3400
|
||||||
|
this.getTimeout = options.getTimeout || 3400
|
||||||
}
|
}
|
||||||
|
|
||||||
close(relays: string[]): void {
|
close(relays: string[]): void {
|
||||||
@@ -24,7 +29,10 @@ export class SimplePool {
|
|||||||
const existing = this._conn[nm]
|
const existing = this._conn[nm]
|
||||||
if (existing) return existing
|
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
|
this._conn[nm] = relay
|
||||||
|
|
||||||
await relay.connect()
|
await relay.connect()
|
||||||
@@ -51,7 +59,7 @@ export class SimplePool {
|
|||||||
let eoseTimeout = setTimeout(() => {
|
let eoseTimeout = setTimeout(() => {
|
||||||
eoseSent = true
|
eoseSent = true
|
||||||
for (let cb of eoseListeners.values()) cb()
|
for (let cb of eoseListeners.values()) cb()
|
||||||
}, 2400)
|
}, this.eoseSubTimeout)
|
||||||
|
|
||||||
relays.forEach(async relay => {
|
relays.forEach(async relay => {
|
||||||
let r
|
let r
|
||||||
@@ -120,7 +128,7 @@ export class SimplePool {
|
|||||||
let timeout = setTimeout(() => {
|
let timeout = setTimeout(() => {
|
||||||
sub.unsub()
|
sub.unsub()
|
||||||
resolve(null)
|
resolve(null)
|
||||||
}, 1500)
|
}, this.getTimeout)
|
||||||
sub.on('event', (event: Event) => {
|
sub.on('event', (event: Event) => {
|
||||||
resolve(event)
|
resolve(event)
|
||||||
clearTimeout(timeout)
|
clearTimeout(timeout)
|
||||||
@@ -150,13 +158,23 @@ export class SimplePool {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
publish(relays: string[], event: Event): Pub[] {
|
publish(relays: string[], event: Event): Pub {
|
||||||
return relays.map(relay => {
|
let pubs = relays.map(relay => {
|
||||||
let r = this._conn[normalizeURL(relay)]
|
let r = this._conn[normalizeURL(relay)]
|
||||||
if (!r) return badPub(relay)
|
if (!r) return badPub(relay)
|
||||||
let s = r.publish(event)
|
return r.publish(event)
|
||||||
return s
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
on(type, cb) {
|
||||||
|
pubs.forEach((pub, i) => {
|
||||||
|
pub.on(type, () => cb(relays[i]))
|
||||||
|
})
|
||||||
|
},
|
||||||
|
off() {
|
||||||
|
// do nothing here, FIXME
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
seenOn(id: string): string[] {
|
seenOn(id: string): string[] {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ const {
|
|||||||
signEvent
|
signEvent
|
||||||
} = require('./lib/nostr.cjs')
|
} = require('./lib/nostr.cjs')
|
||||||
|
|
||||||
let relay = relayInit('wss://nostr-dev.wellorder.net/')
|
let relay = relayInit('wss://relay.damus.io/')
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
relay.connect()
|
relay.connect()
|
||||||
|
|||||||
19
relay.ts
19
relay.ts
@@ -35,7 +35,15 @@ export type SubscriptionOptions = {
|
|||||||
alreadyHaveEvent?: null | ((id: string, relay: string) => boolean)
|
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 ws: WebSocket
|
||||||
var openSubs: {[id: string]: {filters: Filter[]} & SubscriptionOptions} = {}
|
var openSubs: {[id: string]: {filters: Filter[]} & SubscriptionOptions} = {}
|
||||||
var listeners: {
|
var listeners: {
|
||||||
@@ -175,7 +183,10 @@ export function relayInit(url: string): Relay {
|
|||||||
async function trySend(params: [string, ...any]) {
|
async function trySend(params: [string, ...any]) {
|
||||||
let msg = JSON.stringify(params)
|
let msg = JSON.stringify(params)
|
||||||
if (!connected()) {
|
if (!connected()) {
|
||||||
return
|
await new Promise(resolve => setTimeout(resolve, 1000))
|
||||||
|
if (!connected()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
ws.send(msg)
|
ws.send(msg)
|
||||||
@@ -249,7 +260,7 @@ export function relayInit(url: string): Relay {
|
|||||||
let timeout = setTimeout(() => {
|
let timeout = setTimeout(() => {
|
||||||
s.unsub()
|
s.unsub()
|
||||||
resolve(events)
|
resolve(events)
|
||||||
}, 1500)
|
}, listTimeout)
|
||||||
s.on('eose', () => {
|
s.on('eose', () => {
|
||||||
s.unsub()
|
s.unsub()
|
||||||
clearTimeout(timeout)
|
clearTimeout(timeout)
|
||||||
@@ -265,7 +276,7 @@ export function relayInit(url: string): Relay {
|
|||||||
let timeout = setTimeout(() => {
|
let timeout = setTimeout(() => {
|
||||||
s.unsub()
|
s.unsub()
|
||||||
resolve(null)
|
resolve(null)
|
||||||
}, 1500)
|
}, getTimeout)
|
||||||
s.on('event', (event: Event) => {
|
s.on('event', (event: Event) => {
|
||||||
s.unsub()
|
s.unsub()
|
||||||
clearTimeout(timeout)
|
clearTimeout(timeout)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"emitDeclarationOnly": true,
|
"emitDeclarationOnly": true,
|
||||||
"outDir": "dist",
|
"outDir": "lib",
|
||||||
"rootDir": "."
|
"rootDir": "."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user