mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 08:38:50 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c42cd925ce | ||
|
|
43ccb72476 | ||
|
|
b2b7999517 | ||
|
|
a568afc295 | ||
|
|
9bcaed6e60 | ||
|
|
5a9cbbb557 |
12
README.md
12
README.md
@@ -128,7 +128,7 @@ import 'websocket-polyfill'
|
|||||||
### Interacting with multiple relays
|
### Interacting with multiple relays
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import {pool} from 'nostr-tools'
|
import {SimplePool} from 'nostr-tools'
|
||||||
|
|
||||||
const pool = new SimplePool()
|
const pool = new SimplePool()
|
||||||
|
|
||||||
@@ -158,6 +158,11 @@ let events = await pool.list(relays, [{kinds: [0, 1]}])
|
|||||||
let event = await pool.get(relays, {
|
let event = await pool.get(relays, {
|
||||||
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
||||||
})
|
})
|
||||||
|
|
||||||
|
let relaysForEvent = pool.seenOn(
|
||||||
|
'44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'
|
||||||
|
)
|
||||||
|
// relaysForEvent will be an array of URLs from relays a given event was seen on
|
||||||
```
|
```
|
||||||
|
|
||||||
### Querying profile data from a NIP-05 address
|
### Querying profile data from a NIP-05 address
|
||||||
@@ -288,6 +293,11 @@ Please consult the tests or [the source code](https://github.com/fiatjaf/nostr-t
|
|||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Plumbing
|
||||||
|
|
||||||
|
1. Install [`just`](https://just.systems/)
|
||||||
|
2. `just -l`
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
Public domain.
|
Public domain.
|
||||||
|
|||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
29
package.json
29
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.3.1",
|
"version": "1.4.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -9,11 +9,12 @@
|
|||||||
"main": "lib/nostr.cjs.js",
|
"main": "lib/nostr.cjs.js",
|
||||||
"module": "lib/nostr.esm.js",
|
"module": "lib/nostr.esm.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@noble/hashes": "^0.5.7",
|
"@noble/hashes": "1.0.0",
|
||||||
"@noble/secp256k1": "^1.7.0",
|
"@noble/secp256k1": "^1.7.1",
|
||||||
"@scure/base": "^1.1.1",
|
"@scure/base": "^1.1.1",
|
||||||
"@scure/bip32": "^1.1.1",
|
"@scure/bip32": "^1.1.5",
|
||||||
"@scure/bip39": "^1.1.0"
|
"@scure/bip39": "^1.1.1",
|
||||||
|
"prettier": "^2.8.4"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"decentralization",
|
"decentralization",
|
||||||
@@ -23,20 +24,20 @@
|
|||||||
"nostr"
|
"nostr"
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.0.3",
|
"@types/node": "^18.13.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.46.1",
|
"@typescript-eslint/eslint-plugin": "^5.51.0",
|
||||||
"@typescript-eslint/parser": "^5.46.1",
|
"@typescript-eslint/parser": "^5.51.0",
|
||||||
"esbuild": "0.16.9",
|
"esbuild": "0.16.9",
|
||||||
"esbuild-plugin-alias": "^0.2.1",
|
"esbuild-plugin-alias": "^0.2.1",
|
||||||
"eslint": "^8.30.0",
|
"eslint": "^8.33.0",
|
||||||
"eslint-plugin-babel": "^5.3.1",
|
"eslint-plugin-babel": "^5.3.1",
|
||||||
"esm-loader-typescript": "^1.0.1",
|
"esm-loader-typescript": "^1.0.3",
|
||||||
"events": "^3.3.0",
|
"events": "^3.3.0",
|
||||||
"jest": "^29.3.1",
|
"jest": "^29.4.2",
|
||||||
"node-fetch": "2",
|
"node-fetch": "^2.6.9",
|
||||||
"ts-jest": "^29.0.3",
|
"ts-jest": "^29.0.5",
|
||||||
"tsd": "^0.22.0",
|
"tsd": "^0.22.0",
|
||||||
"typescript": "^4.9.4",
|
"typescript": "^4.9.5",
|
||||||
"websocket-polyfill": "^0.0.3"
|
"websocket-polyfill": "^0.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
pool.test.js
12
pool.test.js
@@ -20,7 +20,12 @@ let relays = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await pool.close([...relays, 'wss://nostr-relay.untethr.me'])
|
await pool.close([
|
||||||
|
...relays,
|
||||||
|
'wss://nostr-relay.untethr.me',
|
||||||
|
'wss://offchain.pub',
|
||||||
|
'wss://eden.nostr.land'
|
||||||
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
test('removing duplicates when querying', async () => {
|
test('removing duplicates when querying', async () => {
|
||||||
@@ -120,4 +125,9 @@ test('list()', async () => {
|
|||||||
.reduce((acc, n) => (acc.indexOf(n) !== -1 ? acc : [...acc, n]), [])
|
.reduce((acc, n) => (acc.indexOf(n) !== -1 ? acc : [...acc, n]), [])
|
||||||
.length
|
.length
|
||||||
)
|
)
|
||||||
|
|
||||||
|
let relaysForAllEvents = events
|
||||||
|
.map(event => pool.seenOn(event.id))
|
||||||
|
.reduce((acc, n) => acc.concat(n), [])
|
||||||
|
expect(relaysForAllEvents.length).toBeGreaterThanOrEqual(events.length)
|
||||||
})
|
})
|
||||||
|
|||||||
38
pool.ts
38
pool.ts
@@ -6,10 +6,10 @@ import {SubscriptionOptions, Sub, Pub} from './relay'
|
|||||||
|
|
||||||
export class SimplePool {
|
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
|
||||||
|
|
||||||
constructor(defaultRelays: string[] = []) {
|
constructor() {
|
||||||
this._conn = {}
|
this._conn = {}
|
||||||
defaultRelays.forEach(this.ensureRelay)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async close(relays: string[]): Promise<void> {
|
async close(relays: string[]): Promise<void> {
|
||||||
@@ -37,7 +37,12 @@ export class SimplePool {
|
|||||||
sub(relays: string[], filters: Filter[], opts?: SubscriptionOptions): Sub {
|
sub(relays: string[], filters: Filter[], opts?: SubscriptionOptions): Sub {
|
||||||
let _knownIds: Set<string> = new Set()
|
let _knownIds: Set<string> = new Set()
|
||||||
let modifiedOpts = opts || {}
|
let modifiedOpts = opts || {}
|
||||||
modifiedOpts.alreadyHaveEvent = id => _knownIds.has(id)
|
modifiedOpts.alreadyHaveEvent = (id, url) => {
|
||||||
|
let set = this._seenOn[id] || new Set()
|
||||||
|
set.add(url)
|
||||||
|
this._seenOn[id] = set
|
||||||
|
return _knownIds.has(id)
|
||||||
|
}
|
||||||
|
|
||||||
let subs: Sub[] = []
|
let subs: Sub[] = []
|
||||||
let eventListeners: Set<(event: Event) => void> = new Set()
|
let eventListeners: Set<(event: Event) => void> = new Set()
|
||||||
@@ -47,9 +52,7 @@ export class SimplePool {
|
|||||||
let eoseSent = false
|
let eoseSent = false
|
||||||
let eoseTimeout = setTimeout(() => {
|
let eoseTimeout = setTimeout(() => {
|
||||||
eoseSent = true
|
eoseSent = true
|
||||||
for (let cb of eoseListeners.values()) {
|
for (let cb of eoseListeners.values()) cb()
|
||||||
cb()
|
|
||||||
}
|
|
||||||
}, 2400)
|
}, 2400)
|
||||||
|
|
||||||
relays.forEach(async relay => {
|
relays.forEach(async relay => {
|
||||||
@@ -58,9 +61,7 @@ export class SimplePool {
|
|||||||
let s = r.sub(filters, modifiedOpts)
|
let s = r.sub(filters, modifiedOpts)
|
||||||
s.on('event', (event: Event) => {
|
s.on('event', (event: Event) => {
|
||||||
_knownIds.add(event.id as string)
|
_knownIds.add(event.id as string)
|
||||||
for (let cb of eventListeners.values()) {
|
for (let cb of eventListeners.values()) cb(event)
|
||||||
cb(event)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
s.on('eose', () => {
|
s.on('eose', () => {
|
||||||
if (eoseSent) return
|
if (eoseSent) return
|
||||||
@@ -68,9 +69,7 @@ export class SimplePool {
|
|||||||
eosesMissing--
|
eosesMissing--
|
||||||
if (eosesMissing === 0) {
|
if (eosesMissing === 0) {
|
||||||
clearTimeout(eoseTimeout)
|
clearTimeout(eoseTimeout)
|
||||||
for (let cb of eoseListeners.values()) {
|
for (let cb of eoseListeners.values()) cb()
|
||||||
cb()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
subs.push(s)
|
subs.push(s)
|
||||||
@@ -85,9 +84,14 @@ export class SimplePool {
|
|||||||
subs.forEach(sub => sub.unsub())
|
subs.forEach(sub => sub.unsub())
|
||||||
},
|
},
|
||||||
on(type, cb) {
|
on(type, cb) {
|
||||||
if (type === 'event') {
|
switch (type) {
|
||||||
eventListeners.add(cb)
|
case 'event':
|
||||||
} else if (type === 'eose') eoseListeners.add(cb)
|
eventListeners.add(cb)
|
||||||
|
break
|
||||||
|
case 'eose':
|
||||||
|
eoseListeners.add(cb)
|
||||||
|
break
|
||||||
|
}
|
||||||
},
|
},
|
||||||
off(type, cb) {
|
off(type, cb) {
|
||||||
if (type === 'event') {
|
if (type === 'event') {
|
||||||
@@ -147,6 +151,10 @@ export class SimplePool {
|
|||||||
return s
|
return s
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
seenOn(id: string): string[] {
|
||||||
|
return Array.from(this._seenOn[id]?.values?.() || [])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function badPub(relay: string): Pub {
|
function badPub(relay: string): Pub {
|
||||||
|
|||||||
6
relay.ts
6
relay.ts
@@ -30,9 +30,9 @@ export type Sub = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type SubscriptionOptions = {
|
export type SubscriptionOptions = {
|
||||||
skipVerification?: boolean
|
|
||||||
alreadyHaveEvent?: null | ((id: string) => boolean)
|
|
||||||
id?: string
|
id?: string
|
||||||
|
skipVerification?: boolean
|
||||||
|
alreadyHaveEvent?: null | ((id: string, relay: string) => boolean)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function relayInit(url: string): Relay {
|
export function relayInit(url: string): Relay {
|
||||||
@@ -112,7 +112,7 @@ export function relayInit(url: string): Relay {
|
|||||||
if (
|
if (
|
||||||
so &&
|
so &&
|
||||||
so.alreadyHaveEvent &&
|
so.alreadyHaveEvent &&
|
||||||
so.alreadyHaveEvent(getHex64(json, 'id'))
|
so.alreadyHaveEvent(getHex64(json, 'id'), url)
|
||||||
) {
|
) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user