mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c42cd925ce | ||
|
|
43ccb72476 | ||
|
|
b2b7999517 | ||
|
|
a568afc295 | ||
|
|
9bcaed6e60 | ||
|
|
5a9cbbb557 | ||
|
|
e9acc59809 | ||
|
|
18fe9637b9 | ||
|
|
ff3bf4a51c | ||
|
|
7ff97b5488 | ||
|
|
df169ea42b | ||
|
|
341f2bcb8d | ||
|
|
b2d1dd2110 | ||
|
|
75d7be5a54 | ||
|
|
b5c8255b2f | ||
|
|
4485c8ed5e | ||
|
|
3710866430 | ||
|
|
da59e3ce90 | ||
|
|
cc8e34163d | ||
|
|
9082953ede | ||
|
|
61f397463d |
7
.github/workflows/npm-publish.yml
vendored
7
.github/workflows/npm-publish.yml
vendored
@@ -12,9 +12,10 @@ jobs:
|
|||||||
- uses: actions/setup-node@v3
|
- uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: 18
|
node-version: 18
|
||||||
- run: yarn --ignore-engines
|
- uses: extractions/setup-just@v1
|
||||||
- run: node build.js
|
- run: just install-dependencies
|
||||||
- run: yarn test
|
- run: just build
|
||||||
|
- run: just test
|
||||||
- uses: JS-DevTools/npm-publish@v1
|
- uses: JS-DevTools/npm-publish@v1
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.NPM_TOKEN }}
|
token: ${{ secrets.NPM_TOKEN }}
|
||||||
|
|||||||
13
.github/workflows/test.yml
vendored
13
.github/workflows/test.yml
vendored
@@ -1,7 +1,9 @@
|
|||||||
name: test every commit
|
name: test every commit
|
||||||
on:
|
on:
|
||||||
- push
|
push:
|
||||||
- pull_request
|
branches:
|
||||||
|
- master
|
||||||
|
pull_request:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
@@ -11,6 +13,7 @@ jobs:
|
|||||||
- uses: actions/setup-node@v3
|
- uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: 18
|
node-version: 18
|
||||||
- run: yarn --ignore-engines
|
- uses: extractions/setup-just@v1
|
||||||
- run: node build.js
|
- run: just install-dependencies
|
||||||
- run: yarn test
|
- run: just build
|
||||||
|
- run: just test
|
||||||
|
|||||||
52
README.md
52
README.md
@@ -111,6 +111,11 @@ pub.on('failed', reason => {
|
|||||||
console.log(`failed to publish to ${relay.url}: ${reason}`)
|
console.log(`failed to publish to ${relay.url}: ${reason}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
let events = await relay.list([{kinds: [0, 1]}])
|
||||||
|
let event = await relay.get({
|
||||||
|
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
||||||
|
})
|
||||||
|
|
||||||
await relay.close()
|
await relay.close()
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -120,6 +125,46 @@ To use this on Node.js you first must install `websocket-polyfill` and import it
|
|||||||
import 'websocket-polyfill'
|
import 'websocket-polyfill'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Interacting with multiple relays
|
||||||
|
|
||||||
|
```js
|
||||||
|
import {SimplePool} from 'nostr-tools'
|
||||||
|
|
||||||
|
const pool = new SimplePool()
|
||||||
|
|
||||||
|
let relays = ['wss://relay.example.com', 'wss://relay.example2.com']
|
||||||
|
|
||||||
|
let relay = await pool.ensureRelay('wss://relay.example3.com')
|
||||||
|
|
||||||
|
let subs = pool.sub([...relays, relay], {
|
||||||
|
authors: ['32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
||||||
|
})
|
||||||
|
|
||||||
|
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', () => {
|
||||||
|
// ...
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
let events = await pool.list(relays, [{kinds: [0, 1]}])
|
||||||
|
let event = await pool.get(relays, {
|
||||||
|
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
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@@ -195,7 +240,7 @@ let event = {
|
|||||||
sendEvent(event)
|
sendEvent(event)
|
||||||
|
|
||||||
// on the receiver side
|
// on the receiver side
|
||||||
sub.on('event', (event) => {
|
sub.on('event', event => {
|
||||||
let sender = event.tags.find(([k, v]) => k === 'p' && v && v !== '')[1]
|
let sender = event.tags.find(([k, v]) => k === 'p' && v && v !== '')[1]
|
||||||
pk1 === sender
|
pk1 === sender
|
||||||
let plaintext = await nip04.decrypt(sk2, pk1, event.content)
|
let plaintext = await nip04.decrypt(sk2, pk1, event.content)
|
||||||
@@ -248,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.
|
||||||
|
|||||||
@@ -33,3 +33,17 @@ test('match kind', () => {
|
|||||||
)
|
)
|
||||||
).toBeTruthy()
|
).toBeTruthy()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('match subscription id', () => {
|
||||||
|
expect(fj.getSubscriptionId('["EVENT","",{}]')).toEqual('')
|
||||||
|
expect(fj.getSubscriptionId('["EVENT","_",{}]')).toEqual('_')
|
||||||
|
expect(fj.getSubscriptionId('["EVENT","subname",{}]')).toEqual('subname')
|
||||||
|
expect(fj.getSubscriptionId('["EVENT", "kasjbdjkav", {}]')).toEqual(
|
||||||
|
'kasjbdjkav'
|
||||||
|
)
|
||||||
|
expect(
|
||||||
|
fj.getSubscriptionId(
|
||||||
|
' [ \n\n "EVENT" , \n\n "y4d5ow45gfwoiudfÇA VSADLKAN KLDASB[12312535]SFMZSNJKLH" , {}]'
|
||||||
|
)
|
||||||
|
).toEqual('y4d5ow45gfwoiudfÇA VSADLKAN KLDASB[12312535]SFMZSNJKLH')
|
||||||
|
})
|
||||||
|
|||||||
15
fakejson.ts
15
fakejson.ts
@@ -13,6 +13,21 @@ export function getInt(json: string, field: string): number {
|
|||||||
return parseInt(sliced.slice(0, end), 10)
|
return parseInt(sliced.slice(0, end), 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getSubscriptionId(json: string): string | null {
|
||||||
|
let idx = json.slice(0, 22).indexOf(`"EVENT"`)
|
||||||
|
if (idx === -1) return null
|
||||||
|
|
||||||
|
let pstart = json.slice(idx + 7 + 1).indexOf(`"`)
|
||||||
|
if (pstart === -1) return null
|
||||||
|
let start = idx + 7 + 1 + pstart
|
||||||
|
|
||||||
|
let pend = json.slice(start + 1, 80).indexOf(`"`)
|
||||||
|
if (pend === -1) return null
|
||||||
|
let end = start + 1 + pend
|
||||||
|
|
||||||
|
return json.slice(start + 1, end)
|
||||||
|
}
|
||||||
|
|
||||||
export function matchEventId(json: string, id: string): boolean {
|
export function matchEventId(json: string, id: string): boolean {
|
||||||
return id === getHex64(json, 'id')
|
return id === getHex64(json, 'id')
|
||||||
}
|
}
|
||||||
|
|||||||
1
index.ts
1
index.ts
@@ -2,6 +2,7 @@ export * from './keys'
|
|||||||
export * from './relay'
|
export * from './relay'
|
||||||
export * from './event'
|
export * from './event'
|
||||||
export * from './filter'
|
export * from './filter'
|
||||||
|
export * from './pool'
|
||||||
|
|
||||||
export * as nip04 from './nip04'
|
export * as nip04 from './nip04'
|
||||||
export * as nip05 from './nip05'
|
export * as nip05 from './nip05'
|
||||||
|
|||||||
16
justfile
Normal file
16
justfile
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
export PATH := "./node_modules/.bin:" + env_var('PATH')
|
||||||
|
|
||||||
|
install-dependencies:
|
||||||
|
yarn --ignore-engines
|
||||||
|
|
||||||
|
build:
|
||||||
|
node build.js
|
||||||
|
|
||||||
|
test: build
|
||||||
|
jest
|
||||||
|
|
||||||
|
testOnly file: build
|
||||||
|
jest {{file}}
|
||||||
|
|
||||||
|
publish: build
|
||||||
|
npm publish
|
||||||
2
nip05.ts
2
nip05.ts
@@ -36,7 +36,7 @@ export async function queryProfile(
|
|||||||
name = '_'
|
name = '_'
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!name.match(/^[a-z0-9-_]+$/)) return null
|
if (!name.match(/^[A-Za-z0-9-_]+$/)) return null
|
||||||
|
|
||||||
let res = await (
|
let res = await (
|
||||||
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
||||||
|
|||||||
34
package.json
34
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.2.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,25 +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"
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"build": "node build.js",
|
|
||||||
"pretest": "node build.js",
|
|
||||||
"test": "jest"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
133
pool.test.js
Normal file
133
pool.test.js
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
/* eslint-env jest */
|
||||||
|
|
||||||
|
require('websocket-polyfill')
|
||||||
|
const {
|
||||||
|
SimplePool,
|
||||||
|
generatePrivateKey,
|
||||||
|
getPublicKey,
|
||||||
|
getEventHash,
|
||||||
|
signEvent
|
||||||
|
} = require('./lib/nostr.cjs')
|
||||||
|
|
||||||
|
let pool = new SimplePool()
|
||||||
|
|
||||||
|
let relays = [
|
||||||
|
'wss://nostr-dev.wellorder.net/',
|
||||||
|
'wss://relay.nostr.bg/',
|
||||||
|
'wss://nostr.fmt.wiz.biz/',
|
||||||
|
'wss://relay.nostr.band/',
|
||||||
|
'wss://nostr.zebedee.cloud/'
|
||||||
|
]
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await pool.close([
|
||||||
|
...relays,
|
||||||
|
'wss://nostr-relay.untethr.me',
|
||||||
|
'wss://offchain.pub',
|
||||||
|
'wss://eden.nostr.land'
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('removing duplicates when querying', async () => {
|
||||||
|
let priv = generatePrivateKey()
|
||||||
|
let pub = getPublicKey(priv)
|
||||||
|
|
||||||
|
let sub = pool.sub(relays, [{authors: [pub]}])
|
||||||
|
let received = []
|
||||||
|
|
||||||
|
sub.on('event', event => {
|
||||||
|
// this should be called only once even though we're listening
|
||||||
|
// to multiple relays because the events will be catched and
|
||||||
|
// deduplicated efficiently (without even being parsed)
|
||||||
|
received.push(event)
|
||||||
|
})
|
||||||
|
|
||||||
|
let event = {
|
||||||
|
pubkey: pub,
|
||||||
|
created_at: Math.round(Date.now() / 1000),
|
||||||
|
content: 'test',
|
||||||
|
kind: 22345,
|
||||||
|
tags: []
|
||||||
|
}
|
||||||
|
event.id = getEventHash(event)
|
||||||
|
event.sig = signEvent(event, priv)
|
||||||
|
|
||||||
|
pool.publish(relays, event)
|
||||||
|
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 1500))
|
||||||
|
|
||||||
|
expect(received).toHaveLength(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('same with double querying', async () => {
|
||||||
|
let priv = generatePrivateKey()
|
||||||
|
let pub = getPublicKey(priv)
|
||||||
|
|
||||||
|
let sub1 = pool.sub(relays, [{authors: [pub]}])
|
||||||
|
let sub2 = pool.sub(relays, [{authors: [pub]}])
|
||||||
|
|
||||||
|
let received = []
|
||||||
|
|
||||||
|
sub1.on('event', event => {
|
||||||
|
received.push(event)
|
||||||
|
})
|
||||||
|
|
||||||
|
sub2.on('event', event => {
|
||||||
|
received.push(event)
|
||||||
|
})
|
||||||
|
|
||||||
|
let event = {
|
||||||
|
pubkey: pub,
|
||||||
|
created_at: Math.round(Date.now() / 1000),
|
||||||
|
content: 'test2',
|
||||||
|
kind: 22346,
|
||||||
|
tags: []
|
||||||
|
}
|
||||||
|
event.id = getEventHash(event)
|
||||||
|
event.sig = signEvent(event, priv)
|
||||||
|
|
||||||
|
pool.publish(relays, event)
|
||||||
|
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 1500))
|
||||||
|
|
||||||
|
expect(received).toHaveLength(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('get()', async () => {
|
||||||
|
let event = await pool.get(relays, {
|
||||||
|
ids: ['d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027']
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(event).toHaveProperty(
|
||||||
|
'id',
|
||||||
|
'd7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('list()', async () => {
|
||||||
|
let events = await pool.list(
|
||||||
|
[...relays, 'wss://offchain.pub', 'wss://eden.nostr.land'],
|
||||||
|
[
|
||||||
|
{
|
||||||
|
authors: [
|
||||||
|
'3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d'
|
||||||
|
],
|
||||||
|
kinds: [1],
|
||||||
|
limit: 2
|
||||||
|
}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
// the actual received number will be greater than 2, but there will be no duplicates
|
||||||
|
expect(events.length).toEqual(
|
||||||
|
events
|
||||||
|
.map(evt => evt.id)
|
||||||
|
.reduce((acc, n) => (acc.indexOf(n) !== -1 ? acc : [...acc, n]), [])
|
||||||
|
.length
|
||||||
|
)
|
||||||
|
|
||||||
|
let relaysForAllEvents = events
|
||||||
|
.map(event => pool.seenOn(event.id))
|
||||||
|
.reduce((acc, n) => acc.concat(n), [])
|
||||||
|
expect(relaysForAllEvents.length).toBeGreaterThanOrEqual(events.length)
|
||||||
|
})
|
||||||
167
pool.ts
Normal file
167
pool.ts
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
import {Relay, relayInit} from './relay'
|
||||||
|
import {normalizeURL} from './utils'
|
||||||
|
import {Filter} from './filter'
|
||||||
|
import {Event} from './event'
|
||||||
|
import {SubscriptionOptions, Sub, Pub} from './relay'
|
||||||
|
|
||||||
|
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() {
|
||||||
|
this._conn = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
async close(relays: string[]): Promise<void> {
|
||||||
|
await Promise.all(
|
||||||
|
relays.map(async url => {
|
||||||
|
let relay = this._conn[normalizeURL(url)]
|
||||||
|
if (relay) await relay.close()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
async ensureRelay(url: string): Promise<Relay> {
|
||||||
|
const nm = normalizeURL(url)
|
||||||
|
const existing = this._conn[nm]
|
||||||
|
if (existing) return existing
|
||||||
|
|
||||||
|
const relay = relayInit(nm)
|
||||||
|
this._conn[nm] = relay
|
||||||
|
|
||||||
|
await relay.connect()
|
||||||
|
|
||||||
|
return relay
|
||||||
|
}
|
||||||
|
|
||||||
|
sub(relays: string[], filters: Filter[], opts?: SubscriptionOptions): Sub {
|
||||||
|
let _knownIds: Set<string> = new Set()
|
||||||
|
let modifiedOpts = opts || {}
|
||||||
|
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 eventListeners: Set<(event: Event) => void> = new Set()
|
||||||
|
let eoseListeners: Set<() => void> = new Set()
|
||||||
|
let eosesMissing = relays.length
|
||||||
|
|
||||||
|
let eoseSent = false
|
||||||
|
let eoseTimeout = setTimeout(() => {
|
||||||
|
eoseSent = true
|
||||||
|
for (let cb of eoseListeners.values()) cb()
|
||||||
|
}, 2400)
|
||||||
|
|
||||||
|
relays.forEach(async relay => {
|
||||||
|
let r = await this.ensureRelay(relay)
|
||||||
|
if (!r) return
|
||||||
|
let s = r.sub(filters, modifiedOpts)
|
||||||
|
s.on('event', (event: Event) => {
|
||||||
|
_knownIds.add(event.id as string)
|
||||||
|
for (let cb of eventListeners.values()) cb(event)
|
||||||
|
})
|
||||||
|
s.on('eose', () => {
|
||||||
|
if (eoseSent) return
|
||||||
|
|
||||||
|
eosesMissing--
|
||||||
|
if (eosesMissing === 0) {
|
||||||
|
clearTimeout(eoseTimeout)
|
||||||
|
for (let cb of eoseListeners.values()) cb()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
subs.push(s)
|
||||||
|
})
|
||||||
|
|
||||||
|
let greaterSub: Sub = {
|
||||||
|
sub(filters, opts) {
|
||||||
|
subs.forEach(sub => sub.sub(filters, opts))
|
||||||
|
return greaterSub
|
||||||
|
},
|
||||||
|
unsub() {
|
||||||
|
subs.forEach(sub => sub.unsub())
|
||||||
|
},
|
||||||
|
on(type, cb) {
|
||||||
|
switch (type) {
|
||||||
|
case 'event':
|
||||||
|
eventListeners.add(cb)
|
||||||
|
break
|
||||||
|
case 'eose':
|
||||||
|
eoseListeners.add(cb)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
},
|
||||||
|
off(type, cb) {
|
||||||
|
if (type === 'event') {
|
||||||
|
eventListeners.delete(cb)
|
||||||
|
} else if (type === 'eose') eoseListeners.delete(cb)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return greaterSub
|
||||||
|
}
|
||||||
|
|
||||||
|
get(
|
||||||
|
relays: string[],
|
||||||
|
filter: Filter,
|
||||||
|
opts?: SubscriptionOptions
|
||||||
|
): Promise<Event | null> {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
let sub = this.sub(relays, [filter], opts)
|
||||||
|
let timeout = setTimeout(() => {
|
||||||
|
sub.unsub()
|
||||||
|
resolve(null)
|
||||||
|
}, 1500)
|
||||||
|
sub.on('event', (event: Event) => {
|
||||||
|
resolve(event)
|
||||||
|
clearTimeout(timeout)
|
||||||
|
sub.unsub()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
list(
|
||||||
|
relays: string[],
|
||||||
|
filters: Filter[],
|
||||||
|
opts?: SubscriptionOptions
|
||||||
|
): Promise<Event[]> {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
let events: Event[] = []
|
||||||
|
let sub = this.sub(relays, filters, opts)
|
||||||
|
|
||||||
|
sub.on('event', (event: Event) => {
|
||||||
|
events.push(event)
|
||||||
|
})
|
||||||
|
|
||||||
|
// we can rely on an eose being emitted here because pool.sub() will fake one
|
||||||
|
sub.on('eose', () => {
|
||||||
|
sub.unsub()
|
||||||
|
resolve(events)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
publish(relays: string[], event: Event): Pub[] {
|
||||||
|
return relays.map(relay => {
|
||||||
|
let r = this._conn[normalizeURL(relay)]
|
||||||
|
if (!r) return badPub(relay)
|
||||||
|
let s = r.publish(event)
|
||||||
|
return s
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
seenOn(id: string): string[] {
|
||||||
|
return Array.from(this._seenOn[id]?.values?.() || [])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function badPub(relay: string): Pub {
|
||||||
|
return {
|
||||||
|
on(typ, cb) {
|
||||||
|
if (typ === 'failed') cb(`relay ${relay} not connected`)
|
||||||
|
},
|
||||||
|
off() {}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,7 +32,7 @@ test('connectivity', () => {
|
|||||||
).resolves.toBe(true)
|
).resolves.toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('querying', () => {
|
test('querying', async () => {
|
||||||
var resolve1
|
var resolve1
|
||||||
var resolve2
|
var resolve2
|
||||||
|
|
||||||
@@ -52,16 +52,42 @@ test('querying', () => {
|
|||||||
resolve2(true)
|
resolve2(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
return expect(
|
let [t1, t2] = await Promise.all([
|
||||||
Promise.all([
|
new Promise(resolve => {
|
||||||
new Promise(resolve => {
|
resolve1 = resolve
|
||||||
resolve1 = resolve
|
}),
|
||||||
}),
|
new Promise(resolve => {
|
||||||
new Promise(resolve => {
|
resolve2 = resolve
|
||||||
resolve2 = resolve
|
})
|
||||||
})
|
])
|
||||||
])
|
|
||||||
).resolves.toEqual([true, true])
|
expect(t1).toEqual(true)
|
||||||
|
expect(t2).toEqual(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('get()', async () => {
|
||||||
|
let event = await relay.get({
|
||||||
|
ids: ['d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027']
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(event).toHaveProperty(
|
||||||
|
'id',
|
||||||
|
'd7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('list()', async () => {
|
||||||
|
let events = await relay.list([
|
||||||
|
{
|
||||||
|
authors: [
|
||||||
|
'3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d'
|
||||||
|
],
|
||||||
|
kinds: [1],
|
||||||
|
limit: 2
|
||||||
|
}
|
||||||
|
])
|
||||||
|
|
||||||
|
expect(events.length).toEqual(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('listening (twice) and publishing', async () => {
|
test('listening (twice) and publishing', async () => {
|
||||||
|
|||||||
64
relay.ts
64
relay.ts
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import {Event, verifySignature, validateEvent} from './event'
|
import {Event, verifySignature, validateEvent} from './event'
|
||||||
import {Filter, matchFilters} from './filter'
|
import {Filter, matchFilters} from './filter'
|
||||||
import {getHex64} from './fakejson'
|
import {getHex64, getSubscriptionId} from './fakejson'
|
||||||
|
|
||||||
type RelayEvent = 'connect' | 'disconnect' | 'error' | 'notice'
|
type RelayEvent = 'connect' | 'disconnect' | 'error' | 'notice'
|
||||||
|
|
||||||
@@ -12,6 +12,8 @@ export type Relay = {
|
|||||||
connect: () => Promise<void>
|
connect: () => Promise<void>
|
||||||
close: () => Promise<void>
|
close: () => Promise<void>
|
||||||
sub: (filters: Filter[], opts?: SubscriptionOptions) => Sub
|
sub: (filters: Filter[], opts?: SubscriptionOptions) => Sub
|
||||||
|
list: (filters: Filter[], opts?: SubscriptionOptions) => Promise<Event[]>
|
||||||
|
get: (filter: Filter, opts?: SubscriptionOptions) => Promise<Event | null>
|
||||||
publish: (event: Event) => Pub
|
publish: (event: Event) => Pub
|
||||||
on: (type: RelayEvent, cb: any) => void
|
on: (type: RelayEvent, cb: any) => void
|
||||||
off: (type: RelayEvent, cb: any) => void
|
off: (type: RelayEvent, cb: any) => void
|
||||||
@@ -27,15 +29,13 @@ export type Sub = {
|
|||||||
off: (type: 'event' | 'eose', cb: any) => void
|
off: (type: 'event' | 'eose', cb: any) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
type SubscriptionOptions = {
|
export type SubscriptionOptions = {
|
||||||
skipVerification?: boolean
|
|
||||||
id?: string
|
id?: string
|
||||||
|
skipVerification?: boolean
|
||||||
|
alreadyHaveEvent?: null | ((id: string, relay: string) => boolean)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function relayInit(
|
export function relayInit(url: string): Relay {
|
||||||
url: string,
|
|
||||||
alreadyHaveEvent: (id: string) => boolean = () => false
|
|
||||||
): Relay {
|
|
||||||
var ws: WebSocket
|
var ws: WebSocket
|
||||||
var resolveClose: () => void
|
var resolveClose: () => void
|
||||||
var setOpen: (value: PromiseLike<void> | void) => void
|
var setOpen: (value: PromiseLike<void> | void) => void
|
||||||
@@ -104,8 +104,18 @@ export function relayInit(
|
|||||||
}
|
}
|
||||||
|
|
||||||
var json = incomingMessageQueue.shift()
|
var json = incomingMessageQueue.shift()
|
||||||
if (!json || alreadyHaveEvent(getHex64(json, 'id'))) {
|
if (!json) return
|
||||||
return
|
|
||||||
|
let subid = getSubscriptionId(json)
|
||||||
|
if (subid) {
|
||||||
|
let so = openSubs[subid]
|
||||||
|
if (
|
||||||
|
so &&
|
||||||
|
so.alreadyHaveEvent &&
|
||||||
|
so.alreadyHaveEvent(getHex64(json, 'id'), url)
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -173,6 +183,7 @@ export function relayInit(
|
|||||||
filters: Filter[],
|
filters: Filter[],
|
||||||
{
|
{
|
||||||
skipVerification = false,
|
skipVerification = false,
|
||||||
|
alreadyHaveEvent = null,
|
||||||
id = Math.random().toString().slice(2)
|
id = Math.random().toString().slice(2)
|
||||||
}: SubscriptionOptions = {}
|
}: SubscriptionOptions = {}
|
||||||
): Sub => {
|
): Sub => {
|
||||||
@@ -181,7 +192,8 @@ export function relayInit(
|
|||||||
openSubs[subid] = {
|
openSubs[subid] = {
|
||||||
id: subid,
|
id: subid,
|
||||||
filters,
|
filters,
|
||||||
skipVerification
|
skipVerification,
|
||||||
|
alreadyHaveEvent
|
||||||
}
|
}
|
||||||
trySend(['REQ', subid, ...filters])
|
trySend(['REQ', subid, ...filters])
|
||||||
|
|
||||||
@@ -189,6 +201,7 @@ export function relayInit(
|
|||||||
sub: (newFilters, newOpts = {}) =>
|
sub: (newFilters, newOpts = {}) =>
|
||||||
sub(newFilters || filters, {
|
sub(newFilters || filters, {
|
||||||
skipVerification: newOpts.skipVerification || skipVerification,
|
skipVerification: newOpts.skipVerification || skipVerification,
|
||||||
|
alreadyHaveEvent: newOpts.alreadyHaveEvent || alreadyHaveEvent,
|
||||||
id: subid
|
id: subid
|
||||||
}),
|
}),
|
||||||
unsub: () => {
|
unsub: () => {
|
||||||
@@ -224,6 +237,36 @@ export function relayInit(
|
|||||||
let index = listeners[type].indexOf(cb)
|
let index = listeners[type].indexOf(cb)
|
||||||
if (index !== -1) listeners[type].splice(index, 1)
|
if (index !== -1) listeners[type].splice(index, 1)
|
||||||
},
|
},
|
||||||
|
list: (filters: Filter[], opts?: SubscriptionOptions): Promise<Event[]> =>
|
||||||
|
new Promise(resolve => {
|
||||||
|
let s = sub(filters, opts)
|
||||||
|
let events: Event[] = []
|
||||||
|
let timeout = setTimeout(() => {
|
||||||
|
s.unsub()
|
||||||
|
resolve(events)
|
||||||
|
}, 1500)
|
||||||
|
s.on('eose', () => {
|
||||||
|
s.unsub()
|
||||||
|
clearTimeout(timeout)
|
||||||
|
resolve(events)
|
||||||
|
})
|
||||||
|
s.on('event', (event: Event) => {
|
||||||
|
events.push(event)
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
get: (filter: Filter, opts?: SubscriptionOptions): Promise<Event | null> =>
|
||||||
|
new Promise(resolve => {
|
||||||
|
let s = sub([filter], opts)
|
||||||
|
let timeout = setTimeout(() => {
|
||||||
|
s.unsub()
|
||||||
|
resolve(null)
|
||||||
|
}, 1500)
|
||||||
|
s.on('event', (event: Event) => {
|
||||||
|
s.unsub()
|
||||||
|
clearTimeout(timeout)
|
||||||
|
resolve(event)
|
||||||
|
})
|
||||||
|
}),
|
||||||
publish(event: Event): Pub {
|
publish(event: Event): Pub {
|
||||||
if (!event.id) throw new Error(`event ${event} has no id`)
|
if (!event.id) throw new Error(`event ${event} has no id`)
|
||||||
let id = event.id
|
let id = event.id
|
||||||
@@ -281,6 +324,7 @@ export function relayInit(
|
|||||||
},
|
},
|
||||||
connect,
|
connect,
|
||||||
close(): Promise<void> {
|
close(): Promise<void> {
|
||||||
|
if (ws.readyState > 1) return Promise.resolve()
|
||||||
ws.close()
|
ws.close()
|
||||||
return new Promise<void>(resolve => {
|
return new Promise<void>(resolve => {
|
||||||
resolveClose = resolve
|
resolveClose = resolve
|
||||||
|
|||||||
14
utils.ts
14
utils.ts
@@ -3,6 +3,20 @@ import {Event} from './event'
|
|||||||
export const utf8Decoder = new TextDecoder('utf-8')
|
export const utf8Decoder = new TextDecoder('utf-8')
|
||||||
export const utf8Encoder = new TextEncoder()
|
export const utf8Encoder = new TextEncoder()
|
||||||
|
|
||||||
|
export function normalizeURL(url: string): string {
|
||||||
|
let p = new URL(url)
|
||||||
|
p.pathname = p.pathname.replace(/\/+/g, '/')
|
||||||
|
if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)
|
||||||
|
if (
|
||||||
|
(p.port === '80' && p.protocol === 'ws:') ||
|
||||||
|
(p.port === '443' && p.protocol === 'wss:')
|
||||||
|
)
|
||||||
|
p.port = ''
|
||||||
|
p.searchParams.sort()
|
||||||
|
p.hash = ''
|
||||||
|
return p.toString()
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// fast insert-into-sorted-array functions adapted from https://github.com/terrymorse58/fast-sorted-array
|
// fast insert-into-sorted-array functions adapted from https://github.com/terrymorse58/fast-sorted-array
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user