mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
Compare commits
1 Commits
d3ddd490c2
...
node-webso
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
719462f041 |
@@ -138,7 +138,6 @@
|
|||||||
"valid-typeof": 2,
|
"valid-typeof": 2,
|
||||||
"wrap-iife": [2, "any"],
|
"wrap-iife": [2, "any"],
|
||||||
"yield-star-spacing": [2, "both"],
|
"yield-star-spacing": [2, "both"],
|
||||||
"yoda": [0],
|
"yoda": [0]
|
||||||
"no-labels": [0]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
101
README.md
101
README.md
@@ -133,44 +133,6 @@ import WebSocket from 'ws'
|
|||||||
useWebSocketImplementation(WebSocket)
|
useWebSocketImplementation(WebSocket)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### enablePing
|
|
||||||
|
|
||||||
You can enable regular pings of connected relays with the `enablePing` option. This will set up a heartbeat that closes the websocket if it doesn't receive a response in time. Some platforms, like Node.js, don't report websocket disconnections due to network issues, and enabling this can increase the reliability of the `onclose` event.
|
|
||||||
|
|
||||||
```js
|
|
||||||
import { SimplePool } from 'nostr-tools/pool'
|
|
||||||
|
|
||||||
const pool = new SimplePool({ enablePing: true })
|
|
||||||
```
|
|
||||||
|
|
||||||
#### enableReconnect
|
|
||||||
|
|
||||||
You can also enable automatic reconnection with the `enableReconnect` option. This will make the pool try to reconnect to relays with an exponential backoff delay if the connection is lost unexpectedly.
|
|
||||||
|
|
||||||
```js
|
|
||||||
import { SimplePool } from 'nostr-tools/pool'
|
|
||||||
|
|
||||||
const pool = new SimplePool({ enableReconnect: true })
|
|
||||||
```
|
|
||||||
|
|
||||||
Using both `enablePing: true` and `enableReconnect: true` is recommended as it will improve the reliability and timeliness of the reconnection (at the expense of slighly higher bandwidth due to the ping messages).
|
|
||||||
|
|
||||||
```js
|
|
||||||
// on Node.js
|
|
||||||
const pool = new SimplePool({ enablePing: true, enableReconnect: true })
|
|
||||||
```
|
|
||||||
|
|
||||||
The `enableReconnect` option can also be a callback function which will receive the current subscription filters and should return a new set of filters. This is useful if you want to modify the subscription on reconnect, for example, to update the `since` parameter to fetch only new events.
|
|
||||||
|
|
||||||
```js
|
|
||||||
const pool = new SimplePool({
|
|
||||||
enableReconnect: (filters) => {
|
|
||||||
const newSince = Math.floor(Date.now() / 1000)
|
|
||||||
return filters.map(filter => ({ ...filter, since: newSince }))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
### Parsing references (mentions) from a content based on NIP-27
|
### Parsing references (mentions) from a content based on NIP-27
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@@ -199,10 +161,8 @@ for (let block of nip27.parse(evt.content)) {
|
|||||||
case 'video':
|
case 'video':
|
||||||
case 'audio':
|
case 'audio':
|
||||||
console.log("it's a media url:", block.url)
|
console.log("it's a media url:", block.url)
|
||||||
break
|
|
||||||
case 'relay':
|
case 'relay':
|
||||||
console.log("it's a websocket url, probably a relay address:", block.url)
|
console.log("it's a websocket url, probably a relay address:", block.url)
|
||||||
break
|
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -211,24 +171,14 @@ for (let block of nip27.parse(evt.content)) {
|
|||||||
|
|
||||||
### Connecting to a bunker using NIP-46
|
### Connecting to a bunker using NIP-46
|
||||||
|
|
||||||
`BunkerSigner` allows your application to request signatures and other actions from a remote NIP-46 signer, often called a "bunker". There are two primary ways to establish a connection, depending on whether the client or the bunker initiates the connection.
|
|
||||||
|
|
||||||
A local secret key is required for the client to communicate securely with the bunker. This key should generally be persisted for the user's session.
|
|
||||||
|
|
||||||
```js
|
|
||||||
import { generateSecretKey } from '@nostr/tools/pure'
|
|
||||||
|
|
||||||
const localSecretKey = generateSecretKey()
|
|
||||||
```
|
|
||||||
|
|
||||||
### Method 1: Using a Bunker URI (`bunker://`)
|
|
||||||
|
|
||||||
This is the bunker-initiated flow. Your client receives a `bunker://` string or a NIP-05 identifier from the user. You use `BunkerSigner.fromBunker()` to create an instance, which returns immediately. For the **initial connection** with a new bunker, you must explicitly call `await bunker.connect()` to establish the connection and receive authorization.
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
import { generateSecretKey, getPublicKey } from '@nostr/tools/pure'
|
||||||
import { BunkerSigner, parseBunkerInput } from '@nostr/tools/nip46'
|
import { BunkerSigner, parseBunkerInput } from '@nostr/tools/nip46'
|
||||||
import { SimplePool } from '@nostr/tools/pool'
|
import { SimplePool } from '@nostr/tools/pool'
|
||||||
|
|
||||||
|
// the client needs a local secret key (which is generally persisted) for communicating with the bunker
|
||||||
|
const localSecretKey = generateSecretKey()
|
||||||
|
|
||||||
// parse a bunker URI
|
// parse a bunker URI
|
||||||
const bunkerPointer = await parseBunkerInput('bunker://abcd...?relay=wss://relay.example.com')
|
const bunkerPointer = await parseBunkerInput('bunker://abcd...?relay=wss://relay.example.com')
|
||||||
if (!bunkerPointer) {
|
if (!bunkerPointer) {
|
||||||
@@ -237,7 +187,7 @@ if (!bunkerPointer) {
|
|||||||
|
|
||||||
// create the bunker instance
|
// create the bunker instance
|
||||||
const pool = new SimplePool()
|
const pool = new SimplePool()
|
||||||
const bunker = BunkerSigner.fromBunker(localSecretKey, bunkerPointer, { pool })
|
const bunker = new BunkerSigner(localSecretKey, bunkerPointer, { pool })
|
||||||
await bunker.connect()
|
await bunker.connect()
|
||||||
|
|
||||||
// and use it
|
// and use it
|
||||||
@@ -253,47 +203,6 @@ const event = await bunker.signEvent({
|
|||||||
await signer.close()
|
await signer.close()
|
||||||
pool.close([])
|
pool.close([])
|
||||||
```
|
```
|
||||||
> **Note on Reconnecting:** Once a connection has been successfully established and the `BunkerPointer` is stored, you do **not** need to call `await bunker.connect()` on subsequent sessions.
|
|
||||||
|
|
||||||
### Method 2: Using a Client-generated URI (`nostrconnect://`)
|
|
||||||
|
|
||||||
This is the client-initiated flow, which generally provides a better user experience for first-time connections (e.g., via QR code). Your client generates a `nostrconnect://` URI and waits for the bunker to connect to it.
|
|
||||||
|
|
||||||
`BunkerSigner.fromURI()` is an **asynchronous** method. It returns a `Promise` that resolves only after the bunker has successfully connected. Therefore, the returned signer instance is already fully connected and ready to use, so you **do not** need to call `.connect()` on it.
|
|
||||||
|
|
||||||
```js
|
|
||||||
import { getPublicKey } from '@nostr/tools/pure'
|
|
||||||
import { BunkerSigner, createNostrConnectURI } from '@nostr/tools/nip46'
|
|
||||||
import { SimplePool } from '@nostr/tools/pool'
|
|
||||||
|
|
||||||
const clientPubkey = getPublicKey(localSecretKey)
|
|
||||||
|
|
||||||
// generate a connection URI for the bunker to scan
|
|
||||||
const connectionUri = createNostrConnectURI({
|
|
||||||
clientPubkey,
|
|
||||||
relays: ['wss://relay.damus.io', 'wss://relay.primal.net'],
|
|
||||||
secret: 'a-random-secret-string', // A secret to verify the bunker's response
|
|
||||||
name: 'My Awesome App'
|
|
||||||
})
|
|
||||||
|
|
||||||
// wait for the bunker to connect
|
|
||||||
const pool = new SimplePool()
|
|
||||||
const signer = await BunkerSigner.fromURI(localSecretKey, connectionUri, { pool })
|
|
||||||
|
|
||||||
// and use it
|
|
||||||
const pubkey = await signer.getPublicKey()
|
|
||||||
const event = await signer.signEvent({
|
|
||||||
kind: 1,
|
|
||||||
created_at: Math.floor(Date.now() / 1000),
|
|
||||||
tags: [],
|
|
||||||
content: 'Hello from a client-initiated connection!'
|
|
||||||
})
|
|
||||||
|
|
||||||
// cleanup
|
|
||||||
await signer.close()
|
|
||||||
pool.close([])
|
|
||||||
```
|
|
||||||
> **Note on Persistence:** This method is ideal for the initial sign-in. To allow users to stay logged in across sessions, you should store the connection details and use `Method 1` for subsequent reconnections.
|
|
||||||
|
|
||||||
### Parsing thread from any note based on NIP-10
|
### Parsing thread from any note based on NIP-10
|
||||||
|
|
||||||
|
|||||||
@@ -32,8 +32,6 @@ export class AbstractSimplePool {
|
|||||||
public trackRelays: boolean = false
|
public trackRelays: boolean = false
|
||||||
|
|
||||||
public verifyEvent: Nostr['verifyEvent']
|
public verifyEvent: Nostr['verifyEvent']
|
||||||
public enablePing: boolean | undefined
|
|
||||||
public enableReconnect: boolean | ((filters: Filter[]) => Filter[]) | undefined
|
|
||||||
public trustedRelayURLs: Set<string> = new Set()
|
public trustedRelayURLs: Set<string> = new Set()
|
||||||
|
|
||||||
private _WebSocket?: typeof WebSocket
|
private _WebSocket?: typeof WebSocket
|
||||||
@@ -41,8 +39,6 @@ export class AbstractSimplePool {
|
|||||||
constructor(opts: AbstractPoolConstructorOptions) {
|
constructor(opts: AbstractPoolConstructorOptions) {
|
||||||
this.verifyEvent = opts.verifyEvent
|
this.verifyEvent = opts.verifyEvent
|
||||||
this._WebSocket = opts.websocketImplementation
|
this._WebSocket = opts.websocketImplementation
|
||||||
this.enablePing = opts.enablePing
|
|
||||||
this.enableReconnect = opts.enableReconnect
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async ensureRelay(url: string, params?: { connectionTimeout?: number }): Promise<AbstractRelay> {
|
async ensureRelay(url: string, params?: { connectionTimeout?: number }): Promise<AbstractRelay> {
|
||||||
@@ -53,13 +49,9 @@ export class AbstractSimplePool {
|
|||||||
relay = new AbstractRelay(url, {
|
relay = new AbstractRelay(url, {
|
||||||
verifyEvent: this.trustedRelayURLs.has(url) ? alwaysTrue : this.verifyEvent,
|
verifyEvent: this.trustedRelayURLs.has(url) ? alwaysTrue : this.verifyEvent,
|
||||||
websocketImplementation: this._WebSocket,
|
websocketImplementation: this._WebSocket,
|
||||||
enablePing: this.enablePing,
|
|
||||||
enableReconnect: this.enableReconnect,
|
|
||||||
})
|
})
|
||||||
relay.onclose = () => {
|
relay.onclose = () => {
|
||||||
if (relay && !relay.enableReconnect) {
|
this.relays.delete(url)
|
||||||
this.relays.delete(url)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (params?.connectionTimeout) relay.connectionTimeout = params.connectionTimeout
|
if (params?.connectionTimeout) relay.connectionTimeout = params.connectionTimeout
|
||||||
this.relays.set(url, relay)
|
this.relays.set(url, relay)
|
||||||
@@ -79,44 +71,24 @@ export class AbstractSimplePool {
|
|||||||
subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
||||||
params.onauth = params.onauth || params.doauth
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
const request: { url: string; filter: Filter }[] = []
|
return this.subscribeMap(
|
||||||
for (let i = 0; i < relays.length; i++) {
|
relays.map(url => ({ url, filter })),
|
||||||
const url = normalizeURL(relays[i])
|
params,
|
||||||
if (!request.find(r => r.url === url)) {
|
)
|
||||||
request.push({ url, filter: filter })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.subscribeMap(request, params)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribeMany(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
subscribeMany(relays: string[], filters: Filter[], params: SubscribeManyParams): SubCloser {
|
||||||
params.onauth = params.onauth || params.doauth
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
const request: { url: string; filter: Filter }[] = []
|
return this.subscribeMap(
|
||||||
const uniqUrls: string[] = []
|
relays.flatMap(url => filters.map(filter => ({ url, filter }))),
|
||||||
for (let i = 0; i < relays.length; i++) {
|
params,
|
||||||
const url = normalizeURL(relays[i])
|
)
|
||||||
if (uniqUrls.indexOf(url) === -1) {
|
|
||||||
uniqUrls.push(url)
|
|
||||||
request.push({ url, filter: filter })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.subscribeMap(request, params)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribeMap(requests: { url: string; filter: Filter }[], params: SubscribeManyParams): SubCloser {
|
subscribeMap(requests: { url: string; filter: Filter }[], params: SubscribeManyParams): SubCloser {
|
||||||
params.onauth = params.onauth || params.doauth
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
const grouped = new Map<string, Filter[]>()
|
|
||||||
for (const req of requests) {
|
|
||||||
const { url, filter } = req
|
|
||||||
if (!grouped.has(url)) grouped.set(url, [])
|
|
||||||
grouped.get(url)!.push(filter)
|
|
||||||
}
|
|
||||||
const groupedRequests = Array.from(grouped.entries()).map(([url, filters]) => ({ url, filters }))
|
|
||||||
|
|
||||||
if (this.trackRelays) {
|
if (this.trackRelays) {
|
||||||
params.receivedEvent = (relay: AbstractRelay, id: string) => {
|
params.receivedEvent = (relay: AbstractRelay, id: string) => {
|
||||||
let set = this.seenOn.get(id)
|
let set = this.seenOn.get(id)
|
||||||
@@ -136,7 +108,7 @@ export class AbstractSimplePool {
|
|||||||
let handleEose = (i: number) => {
|
let handleEose = (i: number) => {
|
||||||
if (eosesReceived[i]) return // do not act twice for the same relay
|
if (eosesReceived[i]) return // do not act twice for the same relay
|
||||||
eosesReceived[i] = true
|
eosesReceived[i] = true
|
||||||
if (eosesReceived.filter(a => a).length === groupedRequests.length) {
|
if (eosesReceived.filter(a => a).length === requests.length) {
|
||||||
params.oneose?.()
|
params.oneose?.()
|
||||||
handleEose = () => {}
|
handleEose = () => {}
|
||||||
}
|
}
|
||||||
@@ -147,7 +119,7 @@ export class AbstractSimplePool {
|
|||||||
if (closesReceived[i]) return // do not act twice for the same relay
|
if (closesReceived[i]) return // do not act twice for the same relay
|
||||||
handleEose(i)
|
handleEose(i)
|
||||||
closesReceived[i] = reason
|
closesReceived[i] = reason
|
||||||
if (closesReceived.filter(a => a).length === groupedRequests.length) {
|
if (closesReceived.filter(a => a).length === requests.length) {
|
||||||
params.onclose?.(closesReceived)
|
params.onclose?.(closesReceived)
|
||||||
handleClose = () => {}
|
handleClose = () => {}
|
||||||
}
|
}
|
||||||
@@ -164,7 +136,9 @@ export class AbstractSimplePool {
|
|||||||
|
|
||||||
// open a subscription in all given relays
|
// open a subscription in all given relays
|
||||||
const allOpened = Promise.all(
|
const allOpened = Promise.all(
|
||||||
groupedRequests.map(async ({ url, filters }, i) => {
|
requests.map(async ({ url, filter }, i) => {
|
||||||
|
url = normalizeURL(url)
|
||||||
|
|
||||||
let relay: AbstractRelay
|
let relay: AbstractRelay
|
||||||
try {
|
try {
|
||||||
relay = await this.ensureRelay(url, {
|
relay = await this.ensureRelay(url, {
|
||||||
@@ -175,7 +149,7 @@ export class AbstractSimplePool {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let subscription = relay.subscribe(filters, {
|
let subscription = relay.subscribe([filter], {
|
||||||
...params,
|
...params,
|
||||||
oneose: () => handleEose(i),
|
oneose: () => handleEose(i),
|
||||||
onclose: reason => {
|
onclose: reason => {
|
||||||
@@ -183,7 +157,7 @@ export class AbstractSimplePool {
|
|||||||
relay
|
relay
|
||||||
.auth(params.onauth)
|
.auth(params.onauth)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
relay.subscribe(filters, {
|
relay.subscribe([filter], {
|
||||||
...params,
|
...params,
|
||||||
oneose: () => handleEose(i),
|
oneose: () => handleEose(i),
|
||||||
onclose: reason => {
|
onclose: reason => {
|
||||||
@@ -236,12 +210,12 @@ export class AbstractSimplePool {
|
|||||||
|
|
||||||
subscribeManyEose(
|
subscribeManyEose(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
filter: Filter,
|
filters: Filter[],
|
||||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
||||||
): SubCloser {
|
): SubCloser {
|
||||||
params.onauth = params.onauth || params.doauth
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
const subcloser = this.subscribeMany(relays, filter, {
|
const subcloser = this.subscribeMany(relays, filters, {
|
||||||
...params,
|
...params,
|
||||||
oneose() {
|
oneose() {
|
||||||
subcloser.close('closed automatically on eose')
|
subcloser.close('closed automatically on eose')
|
||||||
|
|||||||
@@ -7,16 +7,22 @@ import { Queue, normalizeURL } from './utils.ts'
|
|||||||
import { makeAuthEvent } from './nip42.ts'
|
import { makeAuthEvent } from './nip42.ts'
|
||||||
import { yieldThread } from './helpers.ts'
|
import { yieldThread } from './helpers.ts'
|
||||||
|
|
||||||
type RelayWebSocket = WebSocket & {
|
export type RelayRecord = Record<string, { read: boolean; write: boolean }>
|
||||||
ping?(): void
|
|
||||||
on?(event: 'pong', listener: () => void): any
|
export interface WebSocketBase {
|
||||||
|
new (url: string | URL, protocols?: string | string[] | undefined): WebSocketBaseConn
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WebSocketBaseConn {
|
||||||
|
onopen: ((this: WebSocketBaseConn, ev: Event) => any) | null
|
||||||
|
onclose: ((this: WebSocketBaseConn) => void) | null
|
||||||
|
onerror: ((this: WebSocketBaseConn, _: MessageEvent<any>) => void) | null
|
||||||
|
onmessage: ((_: MessageEvent<any>) => void) | null
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AbstractRelayConstructorOptions = {
|
export type AbstractRelayConstructorOptions = {
|
||||||
verifyEvent: Nostr['verifyEvent']
|
verifyEvent: Nostr['verifyEvent']
|
||||||
websocketImplementation?: typeof WebSocket
|
websocketImplementation?: WebSocketBase
|
||||||
enablePing?: boolean
|
|
||||||
enableReconnect?: boolean | ((filters: Filter[]) => Filter[])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SendingOnClosedConnection extends Error {
|
export class SendingOnClosedConnection extends Error {
|
||||||
@@ -36,22 +42,13 @@ export class AbstractRelay {
|
|||||||
public baseEoseTimeout: number = 4400
|
public baseEoseTimeout: number = 4400
|
||||||
public connectionTimeout: number = 4400
|
public connectionTimeout: number = 4400
|
||||||
public publishTimeout: number = 4400
|
public publishTimeout: number = 4400
|
||||||
public pingFrequency: number = 20000
|
|
||||||
public pingTimeout: number = 20000
|
|
||||||
public resubscribeBackoff: number[] = [10000, 10000, 10000, 20000, 20000, 30000, 60000]
|
|
||||||
public openSubs: Map<string, Subscription> = new Map()
|
public openSubs: Map<string, Subscription> = new Map()
|
||||||
public enablePing: boolean | undefined
|
|
||||||
public enableReconnect: boolean | ((filters: Filter[]) => Filter[])
|
|
||||||
private connectionTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
private connectionTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
||||||
private reconnectTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
|
||||||
private pingTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
|
||||||
private reconnectAttempts: number = 0
|
|
||||||
private closedIntentionally: boolean = false
|
|
||||||
|
|
||||||
private connectionPromise: Promise<void> | undefined
|
private connectionPromise: Promise<void> | undefined
|
||||||
private openCountRequests = new Map<string, CountResolver>()
|
private openCountRequests = new Map<string, CountResolver>()
|
||||||
private openEventPublishes = new Map<string, EventPublishResolver>()
|
private openEventPublishes = new Map<string, EventPublishResolver>()
|
||||||
private ws: RelayWebSocket | undefined
|
private ws: WebSocket | undefined
|
||||||
private incomingMessageQueue = new Queue<string>()
|
private incomingMessageQueue = new Queue<string>()
|
||||||
private queueRunning = false
|
private queueRunning = false
|
||||||
private challenge: string | undefined
|
private challenge: string | undefined
|
||||||
@@ -59,14 +56,12 @@ export class AbstractRelay {
|
|||||||
private serial: number = 0
|
private serial: number = 0
|
||||||
private verifyEvent: Nostr['verifyEvent']
|
private verifyEvent: Nostr['verifyEvent']
|
||||||
|
|
||||||
private _WebSocket: typeof WebSocket
|
private _WebSocket: WebSocketBase
|
||||||
|
|
||||||
constructor(url: string, opts: AbstractRelayConstructorOptions) {
|
constructor(url: string, opts: AbstractRelayConstructorOptions) {
|
||||||
this.url = normalizeURL(url)
|
this.url = normalizeURL(url)
|
||||||
this.verifyEvent = opts.verifyEvent
|
this.verifyEvent = opts.verifyEvent
|
||||||
this._WebSocket = opts.websocketImplementation || WebSocket
|
this._WebSocket = opts.websocketImplementation || WebSocket
|
||||||
this.enablePing = opts.enablePing
|
|
||||||
this.enableReconnect = opts.enableReconnect || false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async connect(url: string, opts: AbstractRelayConstructorOptions): Promise<AbstractRelay> {
|
static async connect(url: string, opts: AbstractRelayConstructorOptions): Promise<AbstractRelay> {
|
||||||
@@ -96,40 +91,6 @@ export class AbstractRelay {
|
|||||||
return this._connected
|
return this._connected
|
||||||
}
|
}
|
||||||
|
|
||||||
private async reconnect(): Promise<void> {
|
|
||||||
const backoff = this.resubscribeBackoff[Math.min(this.reconnectAttempts, this.resubscribeBackoff.length - 1)]
|
|
||||||
this.reconnectAttempts++
|
|
||||||
|
|
||||||
this.reconnectTimeoutHandle = setTimeout(async () => {
|
|
||||||
try {
|
|
||||||
await this.connect()
|
|
||||||
} catch (err) {
|
|
||||||
// this will be called again through onclose/onerror
|
|
||||||
}
|
|
||||||
}, backoff)
|
|
||||||
}
|
|
||||||
|
|
||||||
private handleHardClose(reason: string) {
|
|
||||||
if (this.pingTimeoutHandle) {
|
|
||||||
clearTimeout(this.pingTimeoutHandle)
|
|
||||||
this.pingTimeoutHandle = undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
this._connected = false
|
|
||||||
this.connectionPromise = undefined
|
|
||||||
|
|
||||||
const wasIntentional = this.closedIntentionally
|
|
||||||
this.closedIntentionally = false // reset for next time
|
|
||||||
|
|
||||||
this.onclose?.()
|
|
||||||
|
|
||||||
if (this.enableReconnect && !wasIntentional) {
|
|
||||||
this.reconnect()
|
|
||||||
} else {
|
|
||||||
this.closeAllSubscriptions(reason)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async connect(): Promise<void> {
|
public async connect(): Promise<void> {
|
||||||
if (this.connectionPromise) return this.connectionPromise
|
if (this.connectionPromise) return this.connectionPromise
|
||||||
|
|
||||||
@@ -152,39 +113,31 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.ws.onopen = () => {
|
this.ws.onopen = () => {
|
||||||
if (this.reconnectTimeoutHandle) {
|
|
||||||
clearTimeout(this.reconnectTimeoutHandle)
|
|
||||||
this.reconnectTimeoutHandle = undefined
|
|
||||||
}
|
|
||||||
clearTimeout(this.connectionTimeoutHandle)
|
clearTimeout(this.connectionTimeoutHandle)
|
||||||
this._connected = true
|
this._connected = true
|
||||||
this.reconnectAttempts = 0
|
|
||||||
|
|
||||||
// resubscribe to all open subscriptions
|
|
||||||
for (const sub of this.openSubs.values()) {
|
|
||||||
sub.eosed = false
|
|
||||||
if (typeof this.enableReconnect === 'function') {
|
|
||||||
sub.filters = this.enableReconnect(sub.filters)
|
|
||||||
}
|
|
||||||
sub.fire()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.enablePing) {
|
|
||||||
this.pingpong()
|
|
||||||
}
|
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
this.ws.onerror = ev => {
|
this.ws.onerror = ev => {
|
||||||
clearTimeout(this.connectionTimeoutHandle)
|
clearTimeout(this.connectionTimeoutHandle)
|
||||||
reject((ev as any).message || 'websocket error')
|
reject((ev as any).message || 'websocket error')
|
||||||
this.handleHardClose('relay connection errored')
|
if (this._connected) {
|
||||||
|
this._connected = false
|
||||||
|
this.connectionPromise = undefined
|
||||||
|
this.onclose?.()
|
||||||
|
this.closeAllSubscriptions('relay connection errored')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.ws.onclose = ev => {
|
this.ws.onclose = ev => {
|
||||||
clearTimeout(this.connectionTimeoutHandle)
|
clearTimeout(this.connectionTimeoutHandle)
|
||||||
reject((ev as any).message || 'websocket closed')
|
reject((ev as any).message || 'websocket closed')
|
||||||
this.handleHardClose('relay connection closed')
|
if (this._connected) {
|
||||||
|
this._connected = false
|
||||||
|
this.connectionPromise = undefined
|
||||||
|
this.onclose?.()
|
||||||
|
this.closeAllSubscriptions('relay connection closed')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.ws.onmessage = this._onmessage.bind(this)
|
this.ws.onmessage = this._onmessage.bind(this)
|
||||||
@@ -193,52 +146,6 @@ export class AbstractRelay {
|
|||||||
return this.connectionPromise
|
return this.connectionPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
private waitForPingPong() {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
// listen for pong
|
|
||||||
;(this.ws as any).once('pong', () => resolve(true))
|
|
||||||
// send a ping
|
|
||||||
this.ws!.ping!()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
private async waitForDummyReq() {
|
|
||||||
return new Promise((resolve, _) => {
|
|
||||||
// make a dummy request with expected empty eose reply
|
|
||||||
// ["REQ", "_", {"ids":["aaaa...aaaa"]}]
|
|
||||||
const sub = this.subscribe([{ ids: ['a'.repeat(64)] }], {
|
|
||||||
oneose: () => {
|
|
||||||
sub.close()
|
|
||||||
resolve(true)
|
|
||||||
},
|
|
||||||
eoseTimeout: this.pingTimeout + 1000,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// nodejs requires this magic here to ensure connections are closed when internet goes off and stuff
|
|
||||||
// in browsers it's done automatically. see https://github.com/nbd-wtf/nostr-tools/issues/491
|
|
||||||
private async pingpong() {
|
|
||||||
// if the websocket is connected
|
|
||||||
if (this.ws?.readyState === 1) {
|
|
||||||
// wait for either a ping-pong reply or a timeout
|
|
||||||
const result = await Promise.any([
|
|
||||||
// browsers don't have ping so use a dummy req
|
|
||||||
this.ws && this.ws.ping && (this.ws as any).once ? this.waitForPingPong() : this.waitForDummyReq(),
|
|
||||||
new Promise(res => setTimeout(() => res(false), this.pingTimeout)),
|
|
||||||
])
|
|
||||||
if (result) {
|
|
||||||
// schedule another pingpong
|
|
||||||
this.pingTimeoutHandle = setTimeout(() => this.pingpong(), this.pingFrequency)
|
|
||||||
} else {
|
|
||||||
// pingpong closing socket
|
|
||||||
if (this.ws?.readyState === this._WebSocket.OPEN) {
|
|
||||||
this.ws?.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async runQueue() {
|
private async runQueue() {
|
||||||
this.queueRunning = true
|
this.queueRunning = true
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -256,7 +163,6 @@ export class AbstractRelay {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// shortcut EVENT sub
|
|
||||||
const subid = getSubscriptionId(json)
|
const subid = getSubscriptionId(json)
|
||||||
if (subid) {
|
if (subid) {
|
||||||
const so = this.openSubs.get(subid as string)
|
const so = this.openSubs.get(subid as string)
|
||||||
@@ -332,19 +238,13 @@ export class AbstractRelay {
|
|||||||
so.close(data[2] as string)
|
so.close(data[2] as string)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case 'NOTICE': {
|
case 'NOTICE':
|
||||||
this.onnotice(data[1] as string)
|
this.onnotice(data[1] as string)
|
||||||
return
|
return
|
||||||
}
|
|
||||||
case 'AUTH': {
|
case 'AUTH': {
|
||||||
this.challenge = data[1] as string
|
this.challenge = data[1] as string
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
default: {
|
|
||||||
const so = this.openSubs.get(data[1])
|
|
||||||
so?.oncustom?.(data)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return
|
return
|
||||||
@@ -412,9 +312,9 @@ export class AbstractRelay {
|
|||||||
filters: Filter[],
|
filters: Filter[],
|
||||||
params: Partial<SubscriptionParams> & { label?: string; id?: string },
|
params: Partial<SubscriptionParams> & { label?: string; id?: string },
|
||||||
): Subscription {
|
): Subscription {
|
||||||
const sub = this.prepareSubscription(filters, params)
|
const subscription = this.prepareSubscription(filters, params)
|
||||||
sub.fire()
|
subscription.fire()
|
||||||
return sub
|
return subscription
|
||||||
}
|
}
|
||||||
|
|
||||||
public prepareSubscription(
|
public prepareSubscription(
|
||||||
@@ -429,21 +329,10 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public close() {
|
public close() {
|
||||||
this.closedIntentionally = true
|
|
||||||
if (this.reconnectTimeoutHandle) {
|
|
||||||
clearTimeout(this.reconnectTimeoutHandle)
|
|
||||||
this.reconnectTimeoutHandle = undefined
|
|
||||||
}
|
|
||||||
if (this.pingTimeoutHandle) {
|
|
||||||
clearTimeout(this.pingTimeoutHandle)
|
|
||||||
this.pingTimeoutHandle = undefined
|
|
||||||
}
|
|
||||||
this.closeAllSubscriptions('relay connection closed by us')
|
this.closeAllSubscriptions('relay connection closed by us')
|
||||||
this._connected = false
|
this._connected = false
|
||||||
|
this.ws?.close()
|
||||||
this.onclose?.()
|
this.onclose?.()
|
||||||
if (this.ws?.readyState === this._WebSocket.OPEN) {
|
|
||||||
this.ws?.close()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is the function assigned to this.ws.onmessage
|
// this is the function assigned to this.ws.onmessage
|
||||||
@@ -470,15 +359,10 @@ export class Subscription {
|
|||||||
public oneose: (() => void) | undefined
|
public oneose: (() => void) | undefined
|
||||||
public onclose: ((reason: string) => void) | undefined
|
public onclose: ((reason: string) => void) | undefined
|
||||||
|
|
||||||
// will get any messages that have this subscription id as their second item and are not default standard
|
|
||||||
public oncustom: ((msg: string[]) => void) | undefined
|
|
||||||
|
|
||||||
public eoseTimeout: number
|
public eoseTimeout: number
|
||||||
private eoseTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
private eoseTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
||||||
|
|
||||||
constructor(relay: AbstractRelay, id: string, filters: Filter[], params: SubscriptionParams) {
|
constructor(relay: AbstractRelay, id: string, filters: Filter[], params: SubscriptionParams) {
|
||||||
if (filters.length === 0) throw new Error("subscription can't be created with zero filters")
|
|
||||||
|
|
||||||
this.relay = relay
|
this.relay = relay
|
||||||
this.filters = filters
|
this.filters = filters
|
||||||
this.id = id
|
this.id = id
|
||||||
|
|||||||
1
build.js
1
build.js
@@ -7,6 +7,7 @@ const entryPoints = fs
|
|||||||
.filter(
|
.filter(
|
||||||
file =>
|
file =>
|
||||||
file.endsWith('.ts') &&
|
file.endsWith('.ts') &&
|
||||||
|
file !== 'core.ts' &&
|
||||||
file !== 'test-helpers.ts' &&
|
file !== 'test-helpers.ts' &&
|
||||||
file !== 'helpers.ts' &&
|
file !== 'helpers.ts' &&
|
||||||
file !== 'benchmarks.ts' &&
|
file !== 'benchmarks.ts' &&
|
||||||
|
|||||||
37
helpers.ts
37
helpers.ts
@@ -1,34 +1,17 @@
|
|||||||
import { verifiedSymbol, type Event, type Nostr, VerifiedEvent } from './core.ts'
|
import { verifiedSymbol, type Event, type Nostr, VerifiedEvent } from './core.ts'
|
||||||
|
|
||||||
export async function yieldThread() {
|
export async function yieldThread() {
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>(resolve => {
|
||||||
try {
|
const ch = new MessageChannel()
|
||||||
// Check if MessageChannel is available
|
const handler = () => {
|
||||||
if (typeof MessageChannel !== 'undefined') {
|
// @ts-ignore (typescript thinks this property should be called `removeListener`, but in fact it's `removeEventListener`)
|
||||||
const ch = new MessageChannel()
|
ch.port1.removeEventListener('message', handler)
|
||||||
const handler = () => {
|
resolve()
|
||||||
// @ts-ignore (typescript thinks this property should be called `removeListener`, but in fact it's `removeEventListener`)
|
|
||||||
ch.port1.removeEventListener('message', handler)
|
|
||||||
resolve()
|
|
||||||
}
|
|
||||||
// @ts-ignore (typescript thinks this property should be called `addListener`, but in fact it's `addEventListener`)
|
|
||||||
ch.port1.addEventListener('message', handler)
|
|
||||||
ch.port2.postMessage(0)
|
|
||||||
ch.port1.start()
|
|
||||||
} else {
|
|
||||||
if (typeof setImmediate !== 'undefined') {
|
|
||||||
setImmediate(resolve)
|
|
||||||
} else if (typeof setTimeout !== 'undefined') {
|
|
||||||
setTimeout(resolve, 0)
|
|
||||||
} else {
|
|
||||||
// Last resort - resolve immediately
|
|
||||||
resolve()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error('during yield: ', e)
|
|
||||||
reject(e)
|
|
||||||
}
|
}
|
||||||
|
// @ts-ignore (typescript thinks this property should be called `addListener`, but in fact it's `addEventListener`)
|
||||||
|
ch.port1.addEventListener('message', handler)
|
||||||
|
ch.port2.postMessage(0)
|
||||||
|
ch.port1.start()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
1
index.ts
1
index.ts
@@ -24,7 +24,6 @@ export * as nip47 from './nip47.ts'
|
|||||||
export * as nip54 from './nip54.ts'
|
export * as nip54 from './nip54.ts'
|
||||||
export * as nip57 from './nip57.ts'
|
export * as nip57 from './nip57.ts'
|
||||||
export * as nip59 from './nip59.ts'
|
export * as nip59 from './nip59.ts'
|
||||||
export * as nip77 from './nip77.ts'
|
|
||||||
export * as nip98 from './nip98.ts'
|
export * as nip98 from './nip98.ts'
|
||||||
|
|
||||||
export * as kinds from './kinds.ts'
|
export * as kinds from './kinds.ts'
|
||||||
|
|||||||
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.18.0",
|
"version": "2.15.1",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./index.ts",
|
".": "./index.ts",
|
||||||
"./core": "./core.ts",
|
"./core": "./core.ts",
|
||||||
|
|||||||
2
justfile
2
justfile
@@ -13,7 +13,7 @@ test-only file:
|
|||||||
|
|
||||||
publish: build
|
publish: build
|
||||||
# publish to jsr first because it is more strict and will catch some errors
|
# publish to jsr first because it is more strict and will catch some errors
|
||||||
perl -i -0pe "s/},\n \"optionalDependencies\": {\n/,/" package.json
|
jq 'delete(.optionalDependencies)' package.json | sponge package.json
|
||||||
jsr publish --allow-dirty
|
jsr publish --allow-dirty
|
||||||
git checkout -- package.json
|
git checkout -- package.json
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { describe, expect, test } from 'bun:test'
|
import { describe, expect, test } from 'bun:test'
|
||||||
// prettier-ignore
|
|
||||||
import {
|
import {
|
||||||
decode,
|
decode,
|
||||||
naddrEncode,
|
naddrEncode,
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { test, expect } from 'bun:test'
|
import { test, expect } from 'bun:test'
|
||||||
import { parse } from './nip27.ts'
|
import { parse } from './nip27.ts'
|
||||||
import { NostrEvent } from './core.ts'
|
|
||||||
|
|
||||||
test('first: parse simple content with 1 url and 1 nostr uri', () => {
|
test('first: parse simple content with 1 url and 1 nostr uri', () => {
|
||||||
const content = `nostr:npub1hpslpc8c5sp3e2nhm2fr7swsfqpys5vyjar5dwpn7e7decps6r8qkcln63 check out my profile:nostr:npub1xtscya34g58tk0z605fvr788k263gsu6cy9x0mhnm87echrgufzsevkk5s; and this cool image https://images.com/image.jpg`
|
const content = `nostr:npub1hpslpc8c5sp3e2nhm2fr7swsfqpys5vyjar5dwpn7e7decps6r8qkcln63 check out my profile:nostr:npub1xtscya34g58tk0z605fvr788k263gsu6cy9x0mhnm87echrgufzsevkk5s; and this cool image https://images.com/image.jpg`
|
||||||
@@ -76,40 +75,3 @@ test('third: parse complex content with 4 nostr uris and 3 urls', () => {
|
|||||||
{ type: 'url', url: 'https://example.com/docs' },
|
{ type: 'url', url: 'https://example.com/docs' },
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
test('parse content with hashtags and emoji shortcodes', () => {
|
|
||||||
const event: NostrEvent = {
|
|
||||||
kind: 1,
|
|
||||||
tags: [
|
|
||||||
['emoji', 'star', 'https://example.com/star.png'],
|
|
||||||
['emoji', 'alpaca', 'https://example.com/alpaca.png'],
|
|
||||||
],
|
|
||||||
content:
|
|
||||||
'hey nostr:npub1hpslpc8c5sp3e2nhm2fr7swsfqpys5vyjar5dwpn7e7decps6r8qkcln63 check out :alpaca::alpaca: #alpaca at wss://alpaca.com! :star:',
|
|
||||||
created_at: 1234567890,
|
|
||||||
pubkey: 'dummy',
|
|
||||||
id: 'dummy',
|
|
||||||
sig: 'dummy',
|
|
||||||
}
|
|
||||||
const blocks = Array.from(parse(event))
|
|
||||||
|
|
||||||
expect(blocks).toEqual([
|
|
||||||
{ type: 'text', text: 'hey ' },
|
|
||||||
{ type: 'reference', pointer: { pubkey: 'b861f0e0f8a4031caa77da923f41d04802485184974746b833f67cdce030d0ce' } },
|
|
||||||
{ type: 'text', text: ' check out ' },
|
|
||||||
{ type: 'emoji', shortcode: 'alpaca', url: 'https://example.com/alpaca.png' },
|
|
||||||
{ type: 'emoji', shortcode: 'alpaca', url: 'https://example.com/alpaca.png' },
|
|
||||||
{ type: 'text', text: ' ' },
|
|
||||||
{ type: 'hashtag', value: 'alpaca' },
|
|
||||||
{ type: 'text', text: ' at ' },
|
|
||||||
{ type: 'relay', url: 'wss://alpaca.com/' },
|
|
||||||
{ type: 'text', text: '! ' },
|
|
||||||
{ type: 'emoji', shortcode: 'star', url: 'https://example.com/star.png' },
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
test('emoji shortcodes are treated as text if no event tags', () => {
|
|
||||||
const blocks = Array.from(parse('hello :alpaca:'))
|
|
||||||
|
|
||||||
expect(blocks).toEqual([{ type: 'text', text: 'hello :alpaca:' }])
|
|
||||||
})
|
|
||||||
|
|||||||
137
nip27.ts
137
nip27.ts
@@ -1,4 +1,3 @@
|
|||||||
import { NostrEvent } from './core.ts'
|
|
||||||
import { AddressPointer, EventPointer, ProfilePointer, decode } from './nip19.ts'
|
import { AddressPointer, EventPointer, ProfilePointer, decode } from './nip19.ts'
|
||||||
|
|
||||||
export type Block =
|
export type Block =
|
||||||
@@ -30,67 +29,27 @@ export type Block =
|
|||||||
type: 'audio'
|
type: 'audio'
|
||||||
url: string
|
url: string
|
||||||
}
|
}
|
||||||
| {
|
|
||||||
type: 'emoji'
|
|
||||||
shortcode: string
|
|
||||||
url: string
|
|
||||||
}
|
|
||||||
| {
|
|
||||||
type: 'hashtag'
|
|
||||||
value: string
|
|
||||||
}
|
|
||||||
|
|
||||||
const noCharacter = /\W/m
|
const noCharacter = /\W/m
|
||||||
const noURLCharacter = /\W |\W$|$|,| /m
|
const noURLCharacter = /\W |\W$|$|,| /m
|
||||||
const MAX_HASHTAG_LENGTH = 42
|
|
||||||
|
|
||||||
export function* parse(content: string | NostrEvent): Iterable<Block> {
|
|
||||||
let emojis: { type: 'emoji'; shortcode: string; url: string }[] = []
|
|
||||||
if (typeof content !== 'string') {
|
|
||||||
for (let i = 0; i < content.tags.length; i++) {
|
|
||||||
const tag = content.tags[i]
|
|
||||||
if (tag[0] === 'emoji' && tag.length >= 3) {
|
|
||||||
emojis.push({ type: 'emoji', shortcode: tag[1], url: tag[2] })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
content = content.content
|
|
||||||
}
|
|
||||||
|
|
||||||
|
export function* parse(content: string): Iterable<Block> {
|
||||||
const max = content.length
|
const max = content.length
|
||||||
let prevIndex = 0
|
let prevIndex = 0
|
||||||
let index = 0
|
let index = 0
|
||||||
mainloop: while (index < max) {
|
while (index < max) {
|
||||||
const u = content.indexOf(':', index)
|
let u = content.indexOf(':', index)
|
||||||
const h = content.indexOf('#', index)
|
if (u === -1) {
|
||||||
if (u === -1 && h === -1) {
|
|
||||||
// reached end
|
// reached end
|
||||||
break mainloop
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if (u === -1 || (h >= 0 && h < u)) {
|
if (content.substring(u - 5, u) === 'nostr') {
|
||||||
// parse hashtag
|
const m = content.substring(u + 60).match(noCharacter)
|
||||||
if (h === 0 || content[h - 1] === ' ') {
|
|
||||||
const m = content.slice(h + 1, h + MAX_HASHTAG_LENGTH).match(noCharacter)
|
|
||||||
const end = m ? h + 1 + m.index! : max
|
|
||||||
yield { type: 'text', text: content.slice(prevIndex, h) }
|
|
||||||
yield { type: 'hashtag', value: content.slice(h + 1, end) }
|
|
||||||
index = end
|
|
||||||
prevIndex = index
|
|
||||||
continue mainloop
|
|
||||||
}
|
|
||||||
|
|
||||||
// ignore this, it is nothing
|
|
||||||
index = h + 1
|
|
||||||
continue mainloop
|
|
||||||
}
|
|
||||||
|
|
||||||
// otherwise parse things that have an ":"
|
|
||||||
if (content.slice(u - 5, u) === 'nostr') {
|
|
||||||
const m = content.slice(u + 60).match(noCharacter)
|
|
||||||
const end = m ? u + 60 + m.index! : max
|
const end = m ? u + 60 + m.index! : max
|
||||||
try {
|
try {
|
||||||
let pointer: ProfilePointer | AddressPointer | EventPointer
|
let pointer: ProfilePointer | AddressPointer | EventPointer
|
||||||
let { data, type } = decode(content.slice(u + 1, end))
|
let { data, type } = decode(content.substring(u + 1, end))
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'npub':
|
case 'npub':
|
||||||
@@ -106,107 +65,105 @@ export function* parse(content: string | NostrEvent): Iterable<Block> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (prevIndex !== u - 5) {
|
if (prevIndex !== u - 5) {
|
||||||
yield { type: 'text', text: content.slice(prevIndex, u - 5) }
|
yield { type: 'text', text: content.substring(prevIndex, u - 5) }
|
||||||
}
|
}
|
||||||
yield { type: 'reference', pointer }
|
yield { type: 'reference', pointer }
|
||||||
index = end
|
index = end
|
||||||
prevIndex = index
|
prevIndex = index
|
||||||
continue mainloop
|
continue
|
||||||
} catch (_err) {
|
} catch (_err) {
|
||||||
// ignore this, not a valid nostr uri
|
// ignore this, not a valid nostr uri
|
||||||
index = u + 1
|
index = u + 1
|
||||||
continue mainloop
|
continue
|
||||||
}
|
}
|
||||||
} else if (content.slice(u - 5, u) === 'https' || content.slice(u - 4, u) === 'http') {
|
} else if (content.substring(u - 5, u) === 'https' || content.substring(u - 4, u) === 'http') {
|
||||||
const m = content.slice(u + 4).match(noURLCharacter)
|
const m = content.substring(u + 4).match(noURLCharacter)
|
||||||
const end = m ? u + 4 + m.index! : max
|
const end = m ? u + 4 + m.index! : max
|
||||||
const prefixLen = content[u - 1] === 's' ? 5 : 4
|
const prefixLen = content[u - 1] === 's' ? 5 : 4
|
||||||
try {
|
try {
|
||||||
let url = new URL(content.slice(u - prefixLen, end))
|
let url = new URL(content.substring(u - prefixLen, end))
|
||||||
if (url.hostname.indexOf('.') === -1) {
|
if (url.hostname.indexOf('.') === -1) {
|
||||||
throw new Error('invalid url')
|
throw new Error('invalid url')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevIndex !== u - prefixLen) {
|
if (prevIndex !== u - prefixLen) {
|
||||||
yield { type: 'text', text: content.slice(prevIndex, u - prefixLen) }
|
yield { type: 'text', text: content.substring(prevIndex, u - prefixLen) }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (/\.(png|jpe?g|gif|webp|heic|svg)$/i.test(url.pathname)) {
|
if (
|
||||||
|
url.pathname.endsWith('.png') ||
|
||||||
|
url.pathname.endsWith('.jpg') ||
|
||||||
|
url.pathname.endsWith('.jpeg') ||
|
||||||
|
url.pathname.endsWith('.gif') ||
|
||||||
|
url.pathname.endsWith('.webp')
|
||||||
|
) {
|
||||||
yield { type: 'image', url: url.toString() }
|
yield { type: 'image', url: url.toString() }
|
||||||
index = end
|
index = end
|
||||||
prevIndex = index
|
prevIndex = index
|
||||||
continue mainloop
|
continue
|
||||||
}
|
}
|
||||||
if (/\.(mp4|avi|webm|mkv|mov)$/i.test(url.pathname)) {
|
if (
|
||||||
|
url.pathname.endsWith('.mp4') ||
|
||||||
|
url.pathname.endsWith('.avi') ||
|
||||||
|
url.pathname.endsWith('.webm') ||
|
||||||
|
url.pathname.endsWith('.mkv')
|
||||||
|
) {
|
||||||
yield { type: 'video', url: url.toString() }
|
yield { type: 'video', url: url.toString() }
|
||||||
index = end
|
index = end
|
||||||
prevIndex = index
|
prevIndex = index
|
||||||
continue mainloop
|
continue
|
||||||
}
|
}
|
||||||
if (/\.(mp3|aac|ogg|opus|wav|flac)$/i.test(url.pathname)) {
|
if (
|
||||||
|
url.pathname.endsWith('.mp3') ||
|
||||||
|
url.pathname.endsWith('.aac') ||
|
||||||
|
url.pathname.endsWith('.ogg') ||
|
||||||
|
url.pathname.endsWith('.opus')
|
||||||
|
) {
|
||||||
yield { type: 'audio', url: url.toString() }
|
yield { type: 'audio', url: url.toString() }
|
||||||
index = end
|
index = end
|
||||||
prevIndex = index
|
prevIndex = index
|
||||||
continue mainloop
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
yield { type: 'url', url: url.toString() }
|
yield { type: 'url', url: url.toString() }
|
||||||
index = end
|
index = end
|
||||||
prevIndex = index
|
prevIndex = index
|
||||||
continue mainloop
|
continue
|
||||||
} catch (_err) {
|
} catch (_err) {
|
||||||
// ignore this, not a valid url
|
// ignore this, not a valid url
|
||||||
index = end + 1
|
index = end + 1
|
||||||
continue mainloop
|
continue
|
||||||
}
|
}
|
||||||
} else if (content.slice(u - 3, u) === 'wss' || content.slice(u - 2, u) === 'ws') {
|
} else if (content.substring(u - 3, u) === 'wss' || content.substring(u - 2, u) === 'ws') {
|
||||||
const m = content.slice(u + 4).match(noURLCharacter)
|
const m = content.substring(u + 4).match(noURLCharacter)
|
||||||
const end = m ? u + 4 + m.index! : max
|
const end = m ? u + 4 + m.index! : max
|
||||||
const prefixLen = content[u - 1] === 's' ? 3 : 2
|
const prefixLen = content[u - 1] === 's' ? 3 : 2
|
||||||
try {
|
try {
|
||||||
let url = new URL(content.slice(u - prefixLen, end))
|
let url = new URL(content.substring(u - prefixLen, end))
|
||||||
if (url.hostname.indexOf('.') === -1) {
|
if (url.hostname.indexOf('.') === -1) {
|
||||||
throw new Error('invalid ws url')
|
throw new Error('invalid ws url')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevIndex !== u - prefixLen) {
|
if (prevIndex !== u - prefixLen) {
|
||||||
yield { type: 'text', text: content.slice(prevIndex, u - prefixLen) }
|
yield { type: 'text', text: content.substring(prevIndex, u - prefixLen) }
|
||||||
}
|
}
|
||||||
yield { type: 'relay', url: url.toString() }
|
yield { type: 'relay', url: url.toString() }
|
||||||
index = end
|
index = end
|
||||||
prevIndex = index
|
prevIndex = index
|
||||||
continue mainloop
|
continue
|
||||||
} catch (_err) {
|
} catch (_err) {
|
||||||
// ignore this, not a valid url
|
// ignore this, not a valid url
|
||||||
index = end + 1
|
index = end + 1
|
||||||
continue mainloop
|
continue
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// try to parse an emoji shortcode
|
|
||||||
for (let e = 0; e < emojis.length; e++) {
|
|
||||||
const emoji = emojis[e]
|
|
||||||
if (
|
|
||||||
content[u + emoji.shortcode.length + 1] === ':' &&
|
|
||||||
content.slice(u + 1, u + emoji.shortcode.length + 1) === emoji.shortcode
|
|
||||||
) {
|
|
||||||
// found an emoji
|
|
||||||
if (prevIndex !== u) {
|
|
||||||
yield { type: 'text', text: content.slice(prevIndex, u) }
|
|
||||||
}
|
|
||||||
yield emoji
|
|
||||||
index = u + emoji.shortcode.length + 2
|
|
||||||
prevIndex = index
|
|
||||||
continue mainloop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ignore this, it is nothing
|
// ignore this, it is nothing
|
||||||
index = u + 1
|
index = u + 1
|
||||||
continue mainloop
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevIndex !== max) {
|
if (prevIndex !== max) {
|
||||||
yield { type: 'text', text: content.slice(prevIndex) }
|
yield { type: 'text', text: content.substring(prevIndex) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
nip29.ts
2
nip29.ts
@@ -2,7 +2,7 @@ import { AbstractSimplePool } from './abstract-pool.ts'
|
|||||||
import { Subscription } from './abstract-relay.ts'
|
import { Subscription } from './abstract-relay.ts'
|
||||||
import type { Event, EventTemplate } from './core.ts'
|
import type { Event, EventTemplate } from './core.ts'
|
||||||
import { fetchRelayInformation, RelayInformation } from './nip11.ts'
|
import { fetchRelayInformation, RelayInformation } from './nip11.ts'
|
||||||
import { decode, NostrTypeGuard } from './nip19.ts'
|
import { AddressPointer, decode, NostrTypeGuard } from './nip19.ts'
|
||||||
import { normalizeURL } from './utils.ts'
|
import { normalizeURL } from './utils.ts'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
211
nip46.ts
211
nip46.ts
@@ -5,6 +5,7 @@ import { getConversationKey, decrypt, encrypt } from './nip44.ts'
|
|||||||
import { NIP05_REGEX } from './nip05.ts'
|
import { NIP05_REGEX } from './nip05.ts'
|
||||||
import { SimplePool } from './pool.ts'
|
import { SimplePool } from './pool.ts'
|
||||||
import { Handlerinformation, NostrConnect } from './kinds.ts'
|
import { Handlerinformation, NostrConnect } from './kinds.ts'
|
||||||
|
import type { RelayRecord } from './relay.ts'
|
||||||
import { Signer } from './signer.ts'
|
import { Signer } from './signer.ts'
|
||||||
|
|
||||||
var _fetch: any
|
var _fetch: any
|
||||||
@@ -77,114 +78,6 @@ export async function queryBunkerProfile(nip05: string): Promise<BunkerPointer |
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type NostrConnectParams = {
|
|
||||||
clientPubkey: string
|
|
||||||
relays: string[]
|
|
||||||
secret: string
|
|
||||||
perms?: string[]
|
|
||||||
name?: string
|
|
||||||
url?: string
|
|
||||||
image?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ParsedNostrConnectURI = {
|
|
||||||
protocol: 'nostrconnect'
|
|
||||||
clientPubkey: string
|
|
||||||
params: {
|
|
||||||
relays: string[]
|
|
||||||
secret: string
|
|
||||||
perms?: string[]
|
|
||||||
name?: string
|
|
||||||
url?: string
|
|
||||||
image?: string
|
|
||||||
}
|
|
||||||
originalString: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createNostrConnectURI(params: NostrConnectParams): string {
|
|
||||||
if (!params.clientPubkey) {
|
|
||||||
throw new Error('clientPubkey is required.')
|
|
||||||
}
|
|
||||||
if (!params.relays || params.relays.length === 0) {
|
|
||||||
throw new Error('At least one relay is required.')
|
|
||||||
}
|
|
||||||
if (!params.secret) {
|
|
||||||
throw new Error('secret is required.')
|
|
||||||
}
|
|
||||||
|
|
||||||
const queryParams = new URLSearchParams()
|
|
||||||
|
|
||||||
params.relays.forEach(relay => {
|
|
||||||
queryParams.append('relay', relay)
|
|
||||||
})
|
|
||||||
|
|
||||||
queryParams.append('secret', params.secret)
|
|
||||||
|
|
||||||
if (params.perms && params.perms.length > 0) {
|
|
||||||
queryParams.append('perms', params.perms.join(','))
|
|
||||||
}
|
|
||||||
if (params.name) {
|
|
||||||
queryParams.append('name', params.name)
|
|
||||||
}
|
|
||||||
if (params.url) {
|
|
||||||
queryParams.append('url', params.url)
|
|
||||||
}
|
|
||||||
if (params.image) {
|
|
||||||
queryParams.append('image', params.image)
|
|
||||||
}
|
|
||||||
|
|
||||||
return `nostrconnect://${params.clientPubkey}?${queryParams.toString()}`
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseNostrConnectURI(uri: string): ParsedNostrConnectURI {
|
|
||||||
if (!uri.startsWith('nostrconnect://')) {
|
|
||||||
throw new Error('Invalid nostrconnect URI: Must start with "nostrconnect://".')
|
|
||||||
}
|
|
||||||
|
|
||||||
const [protocolAndPubkey, queryString] = uri.split('?')
|
|
||||||
if (!protocolAndPubkey || !queryString) {
|
|
||||||
throw new Error('Invalid nostrconnect URI: Missing query string.')
|
|
||||||
}
|
|
||||||
|
|
||||||
const clientPubkey = protocolAndPubkey.substring('nostrconnect://'.length)
|
|
||||||
if (!clientPubkey) {
|
|
||||||
throw new Error('Invalid nostrconnect URI: Missing client-pubkey.')
|
|
||||||
}
|
|
||||||
|
|
||||||
const queryParams = new URLSearchParams(queryString)
|
|
||||||
|
|
||||||
const relays = queryParams.getAll('relay')
|
|
||||||
if (relays.length === 0) {
|
|
||||||
throw new Error('Invalid nostrconnect URI: Missing "relay" parameter.')
|
|
||||||
}
|
|
||||||
|
|
||||||
const secret = queryParams.get('secret')
|
|
||||||
if (!secret) {
|
|
||||||
throw new Error('Invalid nostrconnect URI: Missing "secret" parameter.')
|
|
||||||
}
|
|
||||||
|
|
||||||
const permsString = queryParams.get('perms')
|
|
||||||
const perms = permsString ? permsString.split(',') : undefined
|
|
||||||
|
|
||||||
const name = queryParams.get('name') || undefined
|
|
||||||
const url = queryParams.get('url') || undefined
|
|
||||||
const image = queryParams.get('image') || undefined
|
|
||||||
|
|
||||||
return {
|
|
||||||
protocol: 'nostrconnect',
|
|
||||||
clientPubkey,
|
|
||||||
params: {
|
|
||||||
relays,
|
|
||||||
secret,
|
|
||||||
perms,
|
|
||||||
name,
|
|
||||||
url,
|
|
||||||
image,
|
|
||||||
},
|
|
||||||
originalString: uri,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export type BunkerSignerParams = {
|
export type BunkerSignerParams = {
|
||||||
pool?: AbstractSimplePool
|
pool?: AbstractSimplePool
|
||||||
onauth?: (url: string) => void
|
onauth?: (url: string) => void
|
||||||
@@ -205,9 +98,8 @@ export class BunkerSigner implements Signer {
|
|||||||
}
|
}
|
||||||
private waitingForAuth: { [id: string]: boolean }
|
private waitingForAuth: { [id: string]: boolean }
|
||||||
private secretKey: Uint8Array
|
private secretKey: Uint8Array
|
||||||
// If the client initiates the connection, the two variables below can be filled in later.
|
private conversationKey: Uint8Array
|
||||||
private conversationKey!: Uint8Array
|
public bp: BunkerPointer
|
||||||
public bp!: BunkerPointer
|
|
||||||
|
|
||||||
private cachedPubKey: string | undefined
|
private cachedPubKey: string | undefined
|
||||||
|
|
||||||
@@ -217,95 +109,23 @@ export class BunkerSigner implements Signer {
|
|||||||
* @param remotePubkey - An optional remote public key. This is the key you want to sign as.
|
* @param remotePubkey - An optional remote public key. This is the key you want to sign as.
|
||||||
* @param secretKey - An optional key pair.
|
* @param secretKey - An optional key pair.
|
||||||
*/
|
*/
|
||||||
private constructor(clientSecretKey: Uint8Array, params: BunkerSignerParams) {
|
public constructor(clientSecretKey: Uint8Array, bp: BunkerPointer, params: BunkerSignerParams = {}) {
|
||||||
|
if (bp.relays.length === 0) {
|
||||||
|
throw new Error('no relays are specified for this bunker')
|
||||||
|
}
|
||||||
|
|
||||||
this.params = params
|
this.params = params
|
||||||
this.pool = params.pool || new SimplePool()
|
this.pool = params.pool || new SimplePool()
|
||||||
this.secretKey = clientSecretKey
|
this.secretKey = clientSecretKey
|
||||||
|
this.conversationKey = getConversationKey(clientSecretKey, bp.pubkey)
|
||||||
|
this.bp = bp
|
||||||
this.isOpen = false
|
this.isOpen = false
|
||||||
this.idPrefix = Math.random().toString(36).substring(7)
|
this.idPrefix = Math.random().toString(36).substring(7)
|
||||||
this.serial = 0
|
this.serial = 0
|
||||||
this.listeners = {}
|
this.listeners = {}
|
||||||
this.waitingForAuth = {}
|
this.waitingForAuth = {}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
this.setupSubscription(params)
|
||||||
* [Factory Method 1] Creates a Signer using bunker information (bunker:// URL or NIP-05).
|
|
||||||
* This method is used when the public key of the bunker is known in advance.
|
|
||||||
*/
|
|
||||||
public static fromBunker(
|
|
||||||
clientSecretKey: Uint8Array,
|
|
||||||
bp: BunkerPointer,
|
|
||||||
params: BunkerSignerParams = {},
|
|
||||||
): BunkerSigner {
|
|
||||||
if (bp.relays.length === 0) {
|
|
||||||
throw new Error('No relays specified for this bunker')
|
|
||||||
}
|
|
||||||
|
|
||||||
const signer = new BunkerSigner(clientSecretKey, params)
|
|
||||||
|
|
||||||
signer.conversationKey = getConversationKey(clientSecretKey, bp.pubkey)
|
|
||||||
signer.bp = bp
|
|
||||||
|
|
||||||
signer.setupSubscription(params)
|
|
||||||
return signer
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [Factory Method 2] Creates a Signer using a nostrconnect:// URI generated by the client.
|
|
||||||
* In this method, the bunker initiates the connection by scanning the URI.
|
|
||||||
*/
|
|
||||||
public static async fromURI(
|
|
||||||
clientSecretKey: Uint8Array,
|
|
||||||
connectionURI: string,
|
|
||||||
params: BunkerSignerParams = {},
|
|
||||||
maxWait: number = 300_000,
|
|
||||||
): Promise<BunkerSigner> {
|
|
||||||
const signer = new BunkerSigner(clientSecretKey, params)
|
|
||||||
const parsedURI = parseNostrConnectURI(connectionURI)
|
|
||||||
const clientPubkey = getPublicKey(clientSecretKey)
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const timer = setTimeout(() => {
|
|
||||||
sub.close()
|
|
||||||
reject(new Error(`Connection timed out after ${maxWait / 1000} seconds`))
|
|
||||||
}, maxWait)
|
|
||||||
|
|
||||||
const sub = signer.pool.subscribe(
|
|
||||||
parsedURI.params.relays,
|
|
||||||
{ kinds: [NostrConnect], '#p': [clientPubkey] },
|
|
||||||
{
|
|
||||||
onevent: async (event: NostrEvent) => {
|
|
||||||
try {
|
|
||||||
const tempConvKey = getConversationKey(clientSecretKey, event.pubkey)
|
|
||||||
const decryptedContent = decrypt(event.content, tempConvKey)
|
|
||||||
|
|
||||||
const response = JSON.parse(decryptedContent)
|
|
||||||
|
|
||||||
if (response.result === parsedURI.params.secret) {
|
|
||||||
clearTimeout(timer)
|
|
||||||
sub.close()
|
|
||||||
|
|
||||||
signer.bp = {
|
|
||||||
pubkey: event.pubkey,
|
|
||||||
relays: parsedURI.params.relays,
|
|
||||||
secret: parsedURI.params.secret,
|
|
||||||
}
|
|
||||||
signer.conversationKey = getConversationKey(clientSecretKey, event.pubkey)
|
|
||||||
signer.setupSubscription(params)
|
|
||||||
resolve(signer)
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.warn('Failed to process potential connection event', e)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onclose: () => {
|
|
||||||
clearTimeout(timer)
|
|
||||||
reject(new Error('Subscription closed before connection was established.'))
|
|
||||||
},
|
|
||||||
maxWait,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private setupSubscription(params: BunkerSignerParams) {
|
private setupSubscription(params: BunkerSignerParams) {
|
||||||
@@ -418,6 +238,13 @@ export class BunkerSigner implements Signer {
|
|||||||
return this.cachedPubKey
|
return this.cachedPubKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated removed from NIP
|
||||||
|
*/
|
||||||
|
async getRelays(): Promise<RelayRecord> {
|
||||||
|
return JSON.parse(await this.sendRequest('get_relays', []))
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Signs an event using the remote private key.
|
* Signs an event using the remote private key.
|
||||||
* @param event - The event to sign.
|
* @param event - The event to sign.
|
||||||
@@ -471,7 +298,7 @@ export async function createAccount(
|
|||||||
): Promise<BunkerSigner> {
|
): Promise<BunkerSigner> {
|
||||||
if (email && !EMAIL_REGEX.test(email)) throw new Error('Invalid email')
|
if (email && !EMAIL_REGEX.test(email)) throw new Error('Invalid email')
|
||||||
|
|
||||||
let rpc = BunkerSigner.fromBunker(localSecretKey, bunker.bunkerPointer, params)
|
let rpc = new BunkerSigner(localSecretKey, bunker.bunkerPointer, params)
|
||||||
|
|
||||||
let pubkey = await rpc.sendRequest('create_account', [username, domain, email || ''])
|
let pubkey = await rpc.sendRequest('create_account', [username, domain, email || ''])
|
||||||
|
|
||||||
|
|||||||
109
nip57.test.ts
109
nip57.test.ts
@@ -1,7 +1,112 @@
|
|||||||
import { describe, test, expect } from 'bun:test'
|
import { describe, test, expect, mock } from 'bun:test'
|
||||||
import { finalizeEvent } from './pure.ts'
|
import { finalizeEvent } from './pure.ts'
|
||||||
import { getPublicKey, generateSecretKey } from './pure.ts'
|
import { getPublicKey, generateSecretKey } from './pure.ts'
|
||||||
import { getSatoshisAmountFromBolt11, makeZapReceipt, validateZapRequest } from './nip57.ts'
|
import {
|
||||||
|
getSatoshisAmountFromBolt11,
|
||||||
|
getZapEndpoint,
|
||||||
|
makeZapReceipt,
|
||||||
|
makeZapRequest,
|
||||||
|
useFetchImplementation,
|
||||||
|
validateZapRequest,
|
||||||
|
} from './nip57.ts'
|
||||||
|
import { buildEvent } from './test-helpers.ts'
|
||||||
|
|
||||||
|
describe('getZapEndpoint', () => {
|
||||||
|
test('returns null if neither lud06 nor lud16 is present', async () => {
|
||||||
|
const metadata = buildEvent({ kind: 0, content: '{}' })
|
||||||
|
const result = await getZapEndpoint(metadata)
|
||||||
|
|
||||||
|
expect(result).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns null if fetch fails', async () => {
|
||||||
|
const fetchImplementation = mock(() => Promise.reject(new Error()))
|
||||||
|
useFetchImplementation(fetchImplementation)
|
||||||
|
|
||||||
|
const metadata = buildEvent({ kind: 0, content: '{"lud16": "name@domain"}' })
|
||||||
|
const result = await getZapEndpoint(metadata)
|
||||||
|
|
||||||
|
expect(result).toBeNull()
|
||||||
|
expect(fetchImplementation).toHaveBeenCalledWith('https://domain/.well-known/lnurlp/name')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns null if the response does not allow Nostr payments', async () => {
|
||||||
|
const fetchImplementation = mock(() => Promise.resolve({ json: () => ({ allowsNostr: false }) }))
|
||||||
|
useFetchImplementation(fetchImplementation)
|
||||||
|
|
||||||
|
const metadata = buildEvent({ kind: 0, content: '{"lud16": "name@domain"}' })
|
||||||
|
const result = await getZapEndpoint(metadata)
|
||||||
|
|
||||||
|
expect(result).toBeNull()
|
||||||
|
expect(fetchImplementation).toHaveBeenCalledWith('https://domain/.well-known/lnurlp/name')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns the callback URL if the response allows Nostr payments', async () => {
|
||||||
|
const fetchImplementation = mock(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
json: () => ({
|
||||||
|
allowsNostr: true,
|
||||||
|
nostrPubkey: 'pubkey',
|
||||||
|
callback: 'callback',
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
useFetchImplementation(fetchImplementation)
|
||||||
|
|
||||||
|
const metadata = buildEvent({ kind: 0, content: '{"lud16": "name@domain"}' })
|
||||||
|
const result = await getZapEndpoint(metadata)
|
||||||
|
|
||||||
|
expect(result).toBe('callback')
|
||||||
|
expect(fetchImplementation).toHaveBeenCalledWith('https://domain/.well-known/lnurlp/name')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('makeZapRequest', () => {
|
||||||
|
test('throws an error if amount is not given', () => {
|
||||||
|
expect(() =>
|
||||||
|
// @ts-expect-error
|
||||||
|
makeZapRequest({
|
||||||
|
profile: 'profile',
|
||||||
|
event: null,
|
||||||
|
relays: [],
|
||||||
|
comment: '',
|
||||||
|
}),
|
||||||
|
).toThrow()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('throws an error if profile is not given', () => {
|
||||||
|
expect(() =>
|
||||||
|
// @ts-expect-error
|
||||||
|
makeZapRequest({
|
||||||
|
event: null,
|
||||||
|
amount: 100,
|
||||||
|
relays: [],
|
||||||
|
comment: '',
|
||||||
|
}),
|
||||||
|
).toThrow()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns a valid Zap request', () => {
|
||||||
|
const result = makeZapRequest({
|
||||||
|
profile: 'profile',
|
||||||
|
event: 'event',
|
||||||
|
amount: 100,
|
||||||
|
relays: ['relay1', 'relay2'],
|
||||||
|
comment: 'comment',
|
||||||
|
})
|
||||||
|
expect(result.kind).toBe(9734)
|
||||||
|
expect(result.created_at).toBeCloseTo(Date.now() / 1000, 0)
|
||||||
|
expect(result.content).toBe('comment')
|
||||||
|
expect(result.tags).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
['p', 'profile'],
|
||||||
|
['amount', '100'],
|
||||||
|
['relays', 'relay1', 'relay2'],
|
||||||
|
['e', 'event'],
|
||||||
|
]),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('validateZapRequest', () => {
|
describe('validateZapRequest', () => {
|
||||||
test('returns an error message for invalid JSON', () => {
|
test('returns an error message for invalid JSON', () => {
|
||||||
|
|||||||
62
nip57.ts
62
nip57.ts
@@ -1,6 +1,6 @@
|
|||||||
import { bech32 } from '@scure/base'
|
import { bech32 } from '@scure/base'
|
||||||
|
|
||||||
import { NostrEvent, validateEvent, verifyEvent, type Event, type EventTemplate } from './pure.ts'
|
import { validateEvent, verifyEvent, type Event, type EventTemplate } from './pure.ts'
|
||||||
import { utf8Decoder } from './utils.ts'
|
import { utf8Decoder } from './utils.ts'
|
||||||
import { isReplaceableKind, isAddressableKind } from './kinds.ts'
|
import { isReplaceableKind, isAddressableKind } from './kinds.ts'
|
||||||
|
|
||||||
@@ -18,13 +18,13 @@ export async function getZapEndpoint(metadata: Event): Promise<null | string> {
|
|||||||
try {
|
try {
|
||||||
let lnurl: string = ''
|
let lnurl: string = ''
|
||||||
let { lud06, lud16 } = JSON.parse(metadata.content)
|
let { lud06, lud16 } = JSON.parse(metadata.content)
|
||||||
if (lud16) {
|
if (lud06) {
|
||||||
let [name, domain] = lud16.split('@')
|
|
||||||
lnurl = new URL(`/.well-known/lnurlp/${name}`, `https://${domain}`).toString()
|
|
||||||
} else if (lud06) {
|
|
||||||
let { words } = bech32.decode(lud06, 1000)
|
let { words } = bech32.decode(lud06, 1000)
|
||||||
let data = bech32.fromWords(words)
|
let data = bech32.fromWords(words)
|
||||||
lnurl = utf8Decoder.decode(data)
|
lnurl = utf8Decoder.decode(data)
|
||||||
|
} else if (lud16) {
|
||||||
|
let [name, domain] = lud16.split('@')
|
||||||
|
lnurl = new URL(`/.well-known/lnurlp/${name}`, `https://${domain}`).toString()
|
||||||
} else {
|
} else {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@@ -42,44 +42,48 @@ export async function getZapEndpoint(metadata: Event): Promise<null | string> {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProfileZap = {
|
export function makeZapRequest({
|
||||||
pubkey: string
|
profile,
|
||||||
|
event,
|
||||||
|
amount,
|
||||||
|
relays,
|
||||||
|
comment = '',
|
||||||
|
}: {
|
||||||
|
profile: string
|
||||||
|
event: string | Event | null
|
||||||
amount: number
|
amount: number
|
||||||
comment?: string
|
comment: string
|
||||||
relays: string[]
|
relays: string[]
|
||||||
}
|
}): EventTemplate {
|
||||||
|
if (!amount) throw new Error('amount not given')
|
||||||
|
if (!profile) throw new Error('profile not given')
|
||||||
|
|
||||||
type EventZap = {
|
|
||||||
event: NostrEvent
|
|
||||||
amount: number
|
|
||||||
comment?: string
|
|
||||||
relays: string[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export function makeZapRequest(params: ProfileZap | EventZap): EventTemplate {
|
|
||||||
let zr: EventTemplate = {
|
let zr: EventTemplate = {
|
||||||
kind: 9734,
|
kind: 9734,
|
||||||
created_at: Math.round(Date.now() / 1000),
|
created_at: Math.round(Date.now() / 1000),
|
||||||
content: params.comment || '',
|
content: comment,
|
||||||
tags: [
|
tags: [
|
||||||
['p', 'pubkey' in params ? params.pubkey : params.event.pubkey],
|
['p', profile],
|
||||||
['amount', params.amount.toString()],
|
['amount', amount.toString()],
|
||||||
['relays', ...params.relays],
|
['relays', ...relays],
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('event' in params) {
|
if (event && typeof event === 'string') {
|
||||||
zr.tags.push(['e', params.event.id])
|
zr.tags.push(['e', event])
|
||||||
if (isReplaceableKind(params.event.kind)) {
|
}
|
||||||
const a = ['a', `${params.event.kind}:${params.event.pubkey}:`]
|
if (event && typeof event === 'object') {
|
||||||
|
// replacable event
|
||||||
|
if (isReplaceableKind(event.kind)) {
|
||||||
|
const a = ['a', `${event.kind}:${event.pubkey}:`]
|
||||||
zr.tags.push(a)
|
zr.tags.push(a)
|
||||||
} else if (isAddressableKind(params.event.kind)) {
|
// addressable event
|
||||||
let d = params.event.tags.find(([t, v]) => t === 'd' && v)
|
} else if (isAddressableKind(event.kind)) {
|
||||||
|
let d = event.tags.find(([t, v]) => t === 'd' && v)
|
||||||
if (!d) throw new Error('d tag not found or is empty')
|
if (!d) throw new Error('d tag not found or is empty')
|
||||||
const a = ['a', `${params.event.kind}:${params.event.pubkey}:${d[1]}`]
|
const a = ['a', `${event.kind}:${event.pubkey}:${d[1]}`]
|
||||||
zr.tags.push(a)
|
zr.tags.push(a)
|
||||||
}
|
}
|
||||||
zr.tags.push(['k', params.event.kind.toString()])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return zr
|
return zr
|
||||||
|
|||||||
114
nip77.test.ts
114
nip77.test.ts
@@ -1,114 +0,0 @@
|
|||||||
import { describe, test, expect } from 'bun:test'
|
|
||||||
import { NegentropySync, NegentropyStorageVector } from './nip77.ts'
|
|
||||||
import { Relay } from './relay.ts'
|
|
||||||
import { NostrEvent } from './core.ts'
|
|
||||||
|
|
||||||
// const RELAY = 'ws://127.0.0.1:10547'
|
|
||||||
const RELAY = 'wss://relay.damus.io'
|
|
||||||
|
|
||||||
describe('NegentropySync', () => {
|
|
||||||
test('syncs events from ' + RELAY, async () => {
|
|
||||||
const relay = await Relay.connect(RELAY)
|
|
||||||
|
|
||||||
const storage = new NegentropyStorageVector()
|
|
||||||
storage.seal()
|
|
||||||
const filter = {
|
|
||||||
authors: ['3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d'],
|
|
||||||
kinds: [30617, 30618],
|
|
||||||
}
|
|
||||||
|
|
||||||
let ids1: string[] = []
|
|
||||||
const done1 = Promise.withResolvers<void>()
|
|
||||||
const sync1 = new NegentropySync(relay, storage, filter, {
|
|
||||||
onneed: (id: string) => {
|
|
||||||
ids1.push(id)
|
|
||||||
},
|
|
||||||
onclose: err => {
|
|
||||||
expect(err).toBeUndefined()
|
|
||||||
done1.resolve()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
await sync1.start()
|
|
||||||
await done1.promise
|
|
||||||
|
|
||||||
expect(ids1.length).toBeGreaterThan(10)
|
|
||||||
|
|
||||||
sync1.close()
|
|
||||||
|
|
||||||
// fetch events
|
|
||||||
const events1: NostrEvent[] = []
|
|
||||||
const fetched = Promise.withResolvers()
|
|
||||||
const sub = relay.subscribe([{ ids: ids1 }], {
|
|
||||||
onevent(evt) {
|
|
||||||
events1.push(evt)
|
|
||||||
},
|
|
||||||
oneose() {
|
|
||||||
sub.close()
|
|
||||||
fetched.resolve()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
await fetched.promise
|
|
||||||
expect(events1.map(evt => evt.id).sort()).toEqual(ids1.sort())
|
|
||||||
|
|
||||||
// Second sync with local events
|
|
||||||
await relay.connect()
|
|
||||||
|
|
||||||
const storage2 = new NegentropyStorageVector()
|
|
||||||
for (const evt of events1) {
|
|
||||||
storage2.insert(evt.created_at, evt.id)
|
|
||||||
}
|
|
||||||
storage2.seal()
|
|
||||||
|
|
||||||
let ids2: string[] = []
|
|
||||||
let done2 = Promise.withResolvers()
|
|
||||||
const sync2 = new NegentropySync(relay, storage2, filter, {
|
|
||||||
onneed: (id: string) => {
|
|
||||||
ids2.push(id)
|
|
||||||
},
|
|
||||||
onclose: err => {
|
|
||||||
expect(err).toBeUndefined()
|
|
||||||
done2.resolve()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
await sync2.start()
|
|
||||||
await done2.promise
|
|
||||||
|
|
||||||
expect(ids2.length).toBe(0)
|
|
||||||
|
|
||||||
sync2.close()
|
|
||||||
|
|
||||||
// third sync with 4 events removed
|
|
||||||
const storage3 = new NegentropyStorageVector()
|
|
||||||
|
|
||||||
// shuffle
|
|
||||||
ids1.sort(() => Math.random() - 0.5)
|
|
||||||
const removedEvents = ids1.slice(0, 1 + Math.floor(Math.random() * ids1.length - 1))
|
|
||||||
for (const evt of events1) {
|
|
||||||
if (!removedEvents.includes(evt.id)) {
|
|
||||||
storage3.insert(evt.created_at, evt.id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
storage3.seal()
|
|
||||||
|
|
||||||
let ids3: string[] = []
|
|
||||||
const done3 = Promise.withResolvers()
|
|
||||||
const sync3 = new NegentropySync(relay, storage3, filter, {
|
|
||||||
onneed: (id: string) => {
|
|
||||||
ids3.push(id)
|
|
||||||
},
|
|
||||||
onclose: err => {
|
|
||||||
expect(err).toBeUndefined()
|
|
||||||
done3.resolve()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
await sync3.start()
|
|
||||||
await done3.promise
|
|
||||||
|
|
||||||
expect(ids3.sort()).toEqual(removedEvents.sort())
|
|
||||||
|
|
||||||
sync3.close()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
607
nip77.ts
607
nip77.ts
@@ -1,607 +0,0 @@
|
|||||||
import { bytesToHex, hexToBytes } from '@noble/ciphers/utils'
|
|
||||||
import { Filter } from './filter.ts'
|
|
||||||
import { AbstractRelay, Subscription } from './relay.ts'
|
|
||||||
import { sha256 } from '@noble/hashes/sha256'
|
|
||||||
|
|
||||||
// Negentropy implementation by Doug Hoyte
|
|
||||||
const PROTOCOL_VERSION = 0x61 // Version 1
|
|
||||||
const ID_SIZE = 32
|
|
||||||
const FINGERPRINT_SIZE = 16
|
|
||||||
|
|
||||||
const Mode = {
|
|
||||||
Skip: 0,
|
|
||||||
Fingerprint: 1,
|
|
||||||
IdList: 2,
|
|
||||||
}
|
|
||||||
|
|
||||||
class WrappedBuffer {
|
|
||||||
_raw: Uint8Array
|
|
||||||
length: number
|
|
||||||
|
|
||||||
constructor(buffer?: Uint8Array | number) {
|
|
||||||
if (typeof buffer === 'number') {
|
|
||||||
this._raw = new Uint8Array(buffer)
|
|
||||||
this.length = 0
|
|
||||||
} else if (buffer instanceof Uint8Array) {
|
|
||||||
this._raw = new Uint8Array(buffer)
|
|
||||||
this.length = buffer.length
|
|
||||||
} else {
|
|
||||||
this._raw = new Uint8Array(512)
|
|
||||||
this.length = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unwrap(): Uint8Array {
|
|
||||||
return this._raw.subarray(0, this.length)
|
|
||||||
}
|
|
||||||
|
|
||||||
get capacity(): number {
|
|
||||||
return this._raw.byteLength
|
|
||||||
}
|
|
||||||
|
|
||||||
extend(buf: Uint8Array | WrappedBuffer): void {
|
|
||||||
if (buf instanceof WrappedBuffer) buf = buf.unwrap()
|
|
||||||
if (typeof buf.length !== 'number') throw Error('bad length')
|
|
||||||
const targetSize = buf.length + this.length
|
|
||||||
if (this.capacity < targetSize) {
|
|
||||||
const oldRaw = this._raw
|
|
||||||
const newCapacity = Math.max(this.capacity * 2, targetSize)
|
|
||||||
this._raw = new Uint8Array(newCapacity)
|
|
||||||
this._raw.set(oldRaw)
|
|
||||||
}
|
|
||||||
|
|
||||||
this._raw.set(buf, this.length)
|
|
||||||
this.length += buf.length
|
|
||||||
}
|
|
||||||
|
|
||||||
shift(): number {
|
|
||||||
const first = this._raw[0]
|
|
||||||
this._raw = this._raw.subarray(1)
|
|
||||||
this.length--
|
|
||||||
return first
|
|
||||||
}
|
|
||||||
|
|
||||||
shiftN(n: number = 1): Uint8Array {
|
|
||||||
const firstSubarray = this._raw.subarray(0, n)
|
|
||||||
this._raw = this._raw.subarray(n)
|
|
||||||
this.length -= n
|
|
||||||
return firstSubarray
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function decodeVarInt(buf: WrappedBuffer): number {
|
|
||||||
let res = 0
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
if (buf.length === 0) throw Error('parse ends prematurely')
|
|
||||||
let byte = buf.shift()
|
|
||||||
res = (res << 7) | (byte & 127)
|
|
||||||
if ((byte & 128) === 0) break
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
function encodeVarInt(n: number): WrappedBuffer {
|
|
||||||
if (n === 0) return new WrappedBuffer(new Uint8Array([0]))
|
|
||||||
|
|
||||||
let o: number[] = []
|
|
||||||
|
|
||||||
while (n !== 0) {
|
|
||||||
o.push(n & 127)
|
|
||||||
n >>>= 7
|
|
||||||
}
|
|
||||||
|
|
||||||
o.reverse()
|
|
||||||
|
|
||||||
for (let i = 0; i < o.length - 1; i++) o[i] |= 128
|
|
||||||
|
|
||||||
return new WrappedBuffer(new Uint8Array(o))
|
|
||||||
}
|
|
||||||
|
|
||||||
function getByte(buf: WrappedBuffer): number {
|
|
||||||
return getBytes(buf, 1)[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
function getBytes(buf: WrappedBuffer, n: number): Uint8Array {
|
|
||||||
if (buf.length < n) throw Error('parse ends prematurely')
|
|
||||||
return buf.shiftN(n)
|
|
||||||
}
|
|
||||||
|
|
||||||
class Accumulator {
|
|
||||||
buf!: Uint8Array
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.setToZero()
|
|
||||||
}
|
|
||||||
|
|
||||||
setToZero(): void {
|
|
||||||
this.buf = new Uint8Array(ID_SIZE)
|
|
||||||
}
|
|
||||||
|
|
||||||
add(otherBuf: Uint8Array): void {
|
|
||||||
let currCarry = 0,
|
|
||||||
nextCarry = 0
|
|
||||||
let p = new DataView(this.buf.buffer)
|
|
||||||
let po = new DataView(otherBuf.buffer)
|
|
||||||
|
|
||||||
for (let i = 0; i < 8; i++) {
|
|
||||||
let offset = i * 4
|
|
||||||
let orig = p.getUint32(offset, true)
|
|
||||||
let otherV = po.getUint32(offset, true)
|
|
||||||
|
|
||||||
let next = orig
|
|
||||||
|
|
||||||
next += currCarry
|
|
||||||
next += otherV
|
|
||||||
if (next > 0xffffffff) nextCarry = 1
|
|
||||||
|
|
||||||
p.setUint32(offset, next & 0xffffffff, true)
|
|
||||||
currCarry = nextCarry
|
|
||||||
nextCarry = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
negate(): void {
|
|
||||||
let p = new DataView(this.buf.buffer)
|
|
||||||
|
|
||||||
for (let i = 0; i < 8; i++) {
|
|
||||||
let offset = i * 4
|
|
||||||
p.setUint32(offset, ~p.getUint32(offset, true))
|
|
||||||
}
|
|
||||||
|
|
||||||
let one = new Uint8Array(ID_SIZE)
|
|
||||||
one[0] = 1
|
|
||||||
this.add(one)
|
|
||||||
}
|
|
||||||
|
|
||||||
getFingerprint(n: number): Uint8Array {
|
|
||||||
let input = new WrappedBuffer()
|
|
||||||
input.extend(this.buf)
|
|
||||||
input.extend(encodeVarInt(n))
|
|
||||||
|
|
||||||
let hash = sha256(input.unwrap())
|
|
||||||
return hash.subarray(0, FINGERPRINT_SIZE)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class NegentropyStorageVector {
|
|
||||||
items: { timestamp: number; id: Uint8Array }[]
|
|
||||||
sealed: boolean
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.items = []
|
|
||||||
this.sealed = false
|
|
||||||
}
|
|
||||||
|
|
||||||
insert(timestamp: number, id: string): void {
|
|
||||||
if (this.sealed) throw Error('already sealed')
|
|
||||||
const idb = hexToBytes(id)
|
|
||||||
if (idb.byteLength !== ID_SIZE) throw Error('bad id size for added item')
|
|
||||||
this.items.push({ timestamp, id: idb })
|
|
||||||
}
|
|
||||||
|
|
||||||
seal(): void {
|
|
||||||
if (this.sealed) throw Error('already sealed')
|
|
||||||
this.sealed = true
|
|
||||||
|
|
||||||
this.items.sort(itemCompare)
|
|
||||||
|
|
||||||
for (let i = 1; i < this.items.length; i++) {
|
|
||||||
if (itemCompare(this.items[i - 1], this.items[i]) === 0) throw Error('duplicate item inserted')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unseal(): void {
|
|
||||||
this.sealed = false
|
|
||||||
}
|
|
||||||
|
|
||||||
size(): number {
|
|
||||||
this._checkSealed()
|
|
||||||
return this.items.length
|
|
||||||
}
|
|
||||||
|
|
||||||
getItem(i: number): { timestamp: number; id: Uint8Array } {
|
|
||||||
this._checkSealed()
|
|
||||||
if (i >= this.items.length) throw Error('out of range')
|
|
||||||
return this.items[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
iterate(begin: number, end: number, cb: (item: { timestamp: number; id: Uint8Array }, i: number) => boolean): void {
|
|
||||||
this._checkSealed()
|
|
||||||
this._checkBounds(begin, end)
|
|
||||||
|
|
||||||
for (let i = begin; i < end; ++i) {
|
|
||||||
if (!cb(this.items[i], i)) break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
findLowerBound(begin: number, end: number, bound: { timestamp: number; id: Uint8Array }): number {
|
|
||||||
this._checkSealed()
|
|
||||||
this._checkBounds(begin, end)
|
|
||||||
|
|
||||||
return this._binarySearch(this.items, begin, end, a => itemCompare(a, bound) < 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fingerprint(begin: number, end: number): Uint8Array {
|
|
||||||
let out = new Accumulator()
|
|
||||||
out.setToZero()
|
|
||||||
|
|
||||||
this.iterate(begin, end, item => {
|
|
||||||
out.add(item.id)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
return out.getFingerprint(end - begin)
|
|
||||||
}
|
|
||||||
|
|
||||||
_checkSealed(): void {
|
|
||||||
if (!this.sealed) throw Error('not sealed')
|
|
||||||
}
|
|
||||||
|
|
||||||
_checkBounds(begin: number, end: number): void {
|
|
||||||
if (begin > end || end > this.items.length) throw Error('bad range')
|
|
||||||
}
|
|
||||||
|
|
||||||
_binarySearch(
|
|
||||||
arr: { timestamp: number; id: Uint8Array }[],
|
|
||||||
first: number,
|
|
||||||
last: number,
|
|
||||||
cmp: (a: { timestamp: number; id: Uint8Array }) => boolean,
|
|
||||||
): number {
|
|
||||||
let count = last - first
|
|
||||||
|
|
||||||
while (count > 0) {
|
|
||||||
let it = first
|
|
||||||
let step = Math.floor(count / 2)
|
|
||||||
it += step
|
|
||||||
|
|
||||||
if (cmp(arr[it])) {
|
|
||||||
first = ++it
|
|
||||||
count -= step + 1
|
|
||||||
} else {
|
|
||||||
count = step
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return first
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Negentropy {
|
|
||||||
storage: NegentropyStorageVector
|
|
||||||
frameSizeLimit: number
|
|
||||||
lastTimestampIn: number
|
|
||||||
lastTimestampOut: number
|
|
||||||
|
|
||||||
constructor(storage: NegentropyStorageVector, frameSizeLimit: number = 60_000) {
|
|
||||||
if (frameSizeLimit < 4096) throw Error('frameSizeLimit too small')
|
|
||||||
|
|
||||||
this.storage = storage
|
|
||||||
this.frameSizeLimit = frameSizeLimit
|
|
||||||
|
|
||||||
this.lastTimestampIn = 0
|
|
||||||
this.lastTimestampOut = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
_bound(timestamp: number, id?: Uint8Array): { timestamp: number; id: Uint8Array } {
|
|
||||||
return { timestamp, id: id || new Uint8Array(0) }
|
|
||||||
}
|
|
||||||
|
|
||||||
initiate(): string {
|
|
||||||
let output = new WrappedBuffer()
|
|
||||||
output.extend(new Uint8Array([PROTOCOL_VERSION]))
|
|
||||||
this.splitRange(0, this.storage.size(), this._bound(Number.MAX_VALUE), output)
|
|
||||||
return bytesToHex(output.unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
reconcile(queryMsg: string, onhave?: (id: string) => void, onneed?: (id: string) => void): string | null {
|
|
||||||
const query = new WrappedBuffer(hexToBytes(queryMsg))
|
|
||||||
|
|
||||||
this.lastTimestampIn = this.lastTimestampOut = 0 // reset for each message
|
|
||||||
|
|
||||||
let fullOutput = new WrappedBuffer()
|
|
||||||
fullOutput.extend(new Uint8Array([PROTOCOL_VERSION]))
|
|
||||||
|
|
||||||
let protocolVersion = getByte(query)
|
|
||||||
if (protocolVersion < 0x60 || protocolVersion > 0x6f) throw Error('invalid negentropy protocol version byte')
|
|
||||||
if (protocolVersion !== PROTOCOL_VERSION) {
|
|
||||||
throw Error('unsupported negentropy protocol version requested: ' + (protocolVersion - 0x60))
|
|
||||||
}
|
|
||||||
|
|
||||||
let storageSize = this.storage.size()
|
|
||||||
let prevBound = this._bound(0)
|
|
||||||
let prevIndex = 0
|
|
||||||
let skip = false
|
|
||||||
|
|
||||||
while (query.length !== 0) {
|
|
||||||
let o = new WrappedBuffer()
|
|
||||||
|
|
||||||
let doSkip = () => {
|
|
||||||
if (skip) {
|
|
||||||
skip = false
|
|
||||||
o.extend(this.encodeBound(prevBound))
|
|
||||||
o.extend(encodeVarInt(Mode.Skip))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let currBound = this.decodeBound(query)
|
|
||||||
let mode = decodeVarInt(query)
|
|
||||||
|
|
||||||
let lower = prevIndex
|
|
||||||
let upper = this.storage.findLowerBound(prevIndex, storageSize, currBound)
|
|
||||||
|
|
||||||
if (mode === Mode.Skip) {
|
|
||||||
skip = true
|
|
||||||
} else if (mode === Mode.Fingerprint) {
|
|
||||||
let theirFingerprint = getBytes(query, FINGERPRINT_SIZE)
|
|
||||||
let ourFingerprint = this.storage.fingerprint(lower, upper)
|
|
||||||
|
|
||||||
if (compareUint8Array(theirFingerprint, ourFingerprint) !== 0) {
|
|
||||||
doSkip()
|
|
||||||
this.splitRange(lower, upper, currBound, o)
|
|
||||||
} else {
|
|
||||||
skip = true
|
|
||||||
}
|
|
||||||
} else if (mode === Mode.IdList) {
|
|
||||||
let numIds = decodeVarInt(query)
|
|
||||||
|
|
||||||
let theirElems: { [key: string]: Uint8Array } = {} // stringified Uint8Array -> original Uint8Array (or hex)
|
|
||||||
for (let i = 0; i < numIds; i++) {
|
|
||||||
let e = getBytes(query, ID_SIZE)
|
|
||||||
theirElems[bytesToHex(e)] = e
|
|
||||||
}
|
|
||||||
|
|
||||||
skip = true
|
|
||||||
this.storage.iterate(lower, upper, item => {
|
|
||||||
let k = item.id
|
|
||||||
const id = bytesToHex(k)
|
|
||||||
|
|
||||||
if (!theirElems[id]) {
|
|
||||||
// ID exists on our side, but not their side
|
|
||||||
onhave?.(id)
|
|
||||||
} else {
|
|
||||||
// ID exists on both sides
|
|
||||||
delete theirElems[bytesToHex(k)]
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
if (onneed) {
|
|
||||||
for (let v of Object.values(theirElems)) {
|
|
||||||
// ID exists on their side, but not our side
|
|
||||||
onneed(bytesToHex(v))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw Error('unexpected mode')
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.exceededFrameSizeLimit(fullOutput.length + o.length)) {
|
|
||||||
// frameSizeLimit exceeded: stop range processing and return a fingerprint for the remaining range
|
|
||||||
let remainingFingerprint = this.storage.fingerprint(upper, storageSize)
|
|
||||||
|
|
||||||
fullOutput.extend(this.encodeBound(this._bound(Number.MAX_VALUE)))
|
|
||||||
fullOutput.extend(encodeVarInt(Mode.Fingerprint))
|
|
||||||
fullOutput.extend(remainingFingerprint)
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
fullOutput.extend(o)
|
|
||||||
}
|
|
||||||
|
|
||||||
prevIndex = upper
|
|
||||||
prevBound = currBound
|
|
||||||
}
|
|
||||||
|
|
||||||
return fullOutput.length === 1 ? null : bytesToHex(fullOutput.unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
splitRange(lower: number, upper: number, upperBound: { timestamp: number; id: Uint8Array }, o: WrappedBuffer) {
|
|
||||||
let numElems = upper - lower
|
|
||||||
let buckets = 16
|
|
||||||
|
|
||||||
if (numElems < buckets * 2) {
|
|
||||||
o.extend(this.encodeBound(upperBound))
|
|
||||||
o.extend(encodeVarInt(Mode.IdList))
|
|
||||||
|
|
||||||
o.extend(encodeVarInt(numElems))
|
|
||||||
this.storage.iterate(lower, upper, item => {
|
|
||||||
o.extend(item.id)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
let itemsPerBucket = Math.floor(numElems / buckets)
|
|
||||||
let bucketsWithExtra = numElems % buckets
|
|
||||||
let curr = lower
|
|
||||||
|
|
||||||
for (let i = 0; i < buckets; i++) {
|
|
||||||
let bucketSize = itemsPerBucket + (i < bucketsWithExtra ? 1 : 0)
|
|
||||||
let ourFingerprint = this.storage.fingerprint(curr, curr + bucketSize)
|
|
||||||
curr += bucketSize
|
|
||||||
|
|
||||||
let nextBound: { timestamp: number; id: Uint8Array }
|
|
||||||
|
|
||||||
if (curr === upper) {
|
|
||||||
nextBound = upperBound
|
|
||||||
} else {
|
|
||||||
let prevItem: { timestamp: number; id: Uint8Array } | undefined
|
|
||||||
let currItem: { timestamp: number; id: Uint8Array } | undefined
|
|
||||||
|
|
||||||
this.storage.iterate(curr - 1, curr + 1, (item, index) => {
|
|
||||||
if (index === curr - 1) prevItem = item
|
|
||||||
else currItem = item
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
nextBound = this.getMinimalBound(prevItem!, currItem!)
|
|
||||||
}
|
|
||||||
|
|
||||||
o.extend(this.encodeBound(nextBound))
|
|
||||||
o.extend(encodeVarInt(Mode.Fingerprint))
|
|
||||||
o.extend(ourFingerprint)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
exceededFrameSizeLimit(n: number): boolean {
|
|
||||||
return n > this.frameSizeLimit - 200
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decoding
|
|
||||||
decodeTimestampIn(encoded: WrappedBuffer): number {
|
|
||||||
let timestamp = decodeVarInt(encoded)
|
|
||||||
timestamp = timestamp === 0 ? Number.MAX_VALUE : timestamp - 1
|
|
||||||
if (this.lastTimestampIn === Number.MAX_VALUE || timestamp === Number.MAX_VALUE) {
|
|
||||||
this.lastTimestampIn = Number.MAX_VALUE
|
|
||||||
return Number.MAX_VALUE
|
|
||||||
}
|
|
||||||
timestamp += this.lastTimestampIn
|
|
||||||
this.lastTimestampIn = timestamp
|
|
||||||
return timestamp
|
|
||||||
}
|
|
||||||
|
|
||||||
decodeBound(encoded: WrappedBuffer): { timestamp: number; id: Uint8Array } {
|
|
||||||
let timestamp = this.decodeTimestampIn(encoded)
|
|
||||||
let len = decodeVarInt(encoded)
|
|
||||||
if (len > ID_SIZE) throw Error('bound key too long')
|
|
||||||
let id = getBytes(encoded, len)
|
|
||||||
return { timestamp, id }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encoding
|
|
||||||
encodeTimestampOut(timestamp: number): WrappedBuffer {
|
|
||||||
if (timestamp === Number.MAX_VALUE) {
|
|
||||||
this.lastTimestampOut = Number.MAX_VALUE
|
|
||||||
return encodeVarInt(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
let temp = timestamp
|
|
||||||
timestamp -= this.lastTimestampOut
|
|
||||||
this.lastTimestampOut = temp
|
|
||||||
return encodeVarInt(timestamp + 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
encodeBound(key: { timestamp: number; id: Uint8Array }): WrappedBuffer {
|
|
||||||
let output = new WrappedBuffer()
|
|
||||||
|
|
||||||
output.extend(this.encodeTimestampOut(key.timestamp))
|
|
||||||
output.extend(encodeVarInt(key.id.length))
|
|
||||||
output.extend(key.id)
|
|
||||||
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
|
|
||||||
getMinimalBound(
|
|
||||||
prev: { timestamp: number; id: Uint8Array },
|
|
||||||
curr: { timestamp: number; id: Uint8Array },
|
|
||||||
): { timestamp: number; id: Uint8Array } {
|
|
||||||
if (curr.timestamp !== prev.timestamp) {
|
|
||||||
return this._bound(curr.timestamp)
|
|
||||||
} else {
|
|
||||||
let sharedPrefixBytes = 0
|
|
||||||
let currKey = curr.id
|
|
||||||
let prevKey = prev.id
|
|
||||||
|
|
||||||
for (let i = 0; i < ID_SIZE; i++) {
|
|
||||||
if (currKey[i] !== prevKey[i]) break
|
|
||||||
sharedPrefixBytes++
|
|
||||||
}
|
|
||||||
|
|
||||||
return this._bound(curr.timestamp, curr.id.subarray(0, sharedPrefixBytes + 1))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function compareUint8Array(a: Uint8Array, b: Uint8Array): number {
|
|
||||||
for (let i = 0; i < a.byteLength; i++) {
|
|
||||||
if (a[i] < b[i]) return -1
|
|
||||||
if (a[i] > b[i]) return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
if (a.byteLength > b.byteLength) return 1
|
|
||||||
if (a.byteLength < b.byteLength) return -1
|
|
||||||
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
function itemCompare(a: { timestamp: number; id: Uint8Array }, b: { timestamp: number; id: Uint8Array }): number {
|
|
||||||
if (a.timestamp === b.timestamp) {
|
|
||||||
return compareUint8Array(a.id, b.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
return a.timestamp - b.timestamp
|
|
||||||
}
|
|
||||||
|
|
||||||
export class NegentropySync {
|
|
||||||
relay: AbstractRelay
|
|
||||||
storage: NegentropyStorageVector
|
|
||||||
private neg: Negentropy
|
|
||||||
private filter: Filter
|
|
||||||
private subscription: Subscription
|
|
||||||
private onhave?: (id: string) => void
|
|
||||||
private onneed?: (id: string) => void
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
relay: AbstractRelay,
|
|
||||||
storage: NegentropyStorageVector,
|
|
||||||
filter: Filter,
|
|
||||||
params: {
|
|
||||||
label?: string
|
|
||||||
onhave?: (id: string) => void
|
|
||||||
onneed?: (id: string) => void
|
|
||||||
onclose?: (errReason?: string) => void
|
|
||||||
} = {},
|
|
||||||
) {
|
|
||||||
this.relay = relay
|
|
||||||
this.storage = storage
|
|
||||||
this.neg = new Negentropy(storage)
|
|
||||||
this.onhave = params.onhave
|
|
||||||
this.onneed = params.onneed
|
|
||||||
this.filter = filter
|
|
||||||
|
|
||||||
// we prepare a subscription with an empty filter, but it will not be used
|
|
||||||
this.subscription = this.relay.prepareSubscription([{}], { label: params.label || 'negentropy' })
|
|
||||||
this.subscription.oncustom = (data: string[]) => {
|
|
||||||
switch (data[0]) {
|
|
||||||
case 'NEG-MSG': {
|
|
||||||
if (data.length < 3) {
|
|
||||||
console.warn(`got invalid NEG-MSG from ${this.relay.url}: ${data}`)
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const response = this.neg.reconcile(data[2], this.onhave, this.onneed)
|
|
||||||
if (response) {
|
|
||||||
this.relay.send(`["NEG-MSG", "${this.subscription.id}", "${response}"]`)
|
|
||||||
} else {
|
|
||||||
this.close()
|
|
||||||
params.onclose?.()
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('negentropy reconcile error:', error)
|
|
||||||
params?.onclose?.(`reconcile error: ${error}`)
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
case 'NEG-CLOSE': {
|
|
||||||
const reason = data[2]
|
|
||||||
console.warn('negentropy error:', reason)
|
|
||||||
params.onclose?.(reason)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
case 'NEG-ERR': {
|
|
||||||
params.onclose?.()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async start(): Promise<void> {
|
|
||||||
const initMsg = this.neg.initiate()
|
|
||||||
this.relay.send(`["NEG-OPEN","${this.subscription.id}",${JSON.stringify(this.filter)},"${initMsg}"]`)
|
|
||||||
}
|
|
||||||
|
|
||||||
close(): void {
|
|
||||||
this.relay.send(`["NEG-CLOSE","${this.subscription.id}"]`)
|
|
||||||
this.subscription.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
29
node-ws-relay.ts
Normal file
29
node-ws-relay.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import WebSocket from 'ws'
|
||||||
|
import { verifyEvent } from './pure.ts'
|
||||||
|
import { AbstractRelay, WebSocketBase } from './abstract-relay.ts'
|
||||||
|
|
||||||
|
class NodeWs extends WebSocket implements WebSocketBase {
|
||||||
|
constructor(url: string | URL, protocols?: string | string[] | undefined) {
|
||||||
|
super(url, {
|
||||||
|
protocol: Array.isArray(protocols) ? protocols[0] : protocols,
|
||||||
|
})
|
||||||
|
|
||||||
|
setInterval(() => {
|
||||||
|
this.ping()
|
||||||
|
}, 29000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NodeWsRelay extends AbstractRelay {
|
||||||
|
constructor(url: string) {
|
||||||
|
super(url, { verifyEvent, websocketImplementation: NodeWs })
|
||||||
|
}
|
||||||
|
|
||||||
|
static async connect(url: string): Promise<NodeWsRelay> {
|
||||||
|
const relay = new NodeWsRelay(url)
|
||||||
|
await relay.connect()
|
||||||
|
return relay
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export * from './abstract-relay.ts'
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.18.0",
|
"version": "2.15.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -241,12 +241,15 @@
|
|||||||
"@noble/hashes": "1.3.1",
|
"@noble/hashes": "1.3.1",
|
||||||
"@scure/base": "1.1.1",
|
"@scure/base": "1.1.1",
|
||||||
"@scure/bip32": "1.3.1",
|
"@scure/bip32": "1.3.1",
|
||||||
"@scure/bip39": "1.2.1",
|
"@scure/bip39": "1.2.1"
|
||||||
"nostr-wasm": "0.1.0"
|
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"typescript": ">=5.0.0"
|
"typescript": ">=5.0.0"
|
||||||
},
|
},
|
||||||
|
"optionalDependencies": {
|
||||||
|
"nostr-wasm": "0.1.0",
|
||||||
|
"ws": "^8.18.3"
|
||||||
|
},
|
||||||
"peerDependenciesMeta": {
|
"peerDependenciesMeta": {
|
||||||
"typescript": {
|
"typescript": {
|
||||||
"optional": true
|
"optional": true
|
||||||
|
|||||||
201
pool.test.ts
201
pool.test.ts
@@ -35,18 +35,14 @@ test('removing duplicates when subscribing', async () => {
|
|||||||
priv,
|
priv,
|
||||||
)
|
)
|
||||||
|
|
||||||
pool.subscribeMany(
|
pool.subscribeMany(relayURLs, [{ authors: [pub] }], {
|
||||||
relayURLs,
|
onevent(event: Event) {
|
||||||
{ authors: [pub] },
|
// this should be called only once even though we're listening
|
||||||
{
|
// to multiple relays because the events will be caught and
|
||||||
onevent(event: Event) {
|
// deduplicated efficiently (without even being parsed)
|
||||||
// this should be called only once even though we're listening
|
received.push(event)
|
||||||
// to multiple relays because the events will be caught and
|
|
||||||
// deduplicated efficiently (without even being parsed)
|
|
||||||
received.push(event)
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
)
|
})
|
||||||
|
|
||||||
await Promise.any(pool.publish(relayURLs, event))
|
await Promise.any(pool.publish(relayURLs, event))
|
||||||
await new Promise(resolve => setTimeout(resolve, 200)) // wait for the new published event to be received
|
await new Promise(resolve => setTimeout(resolve, 200)) // wait for the new published event to be received
|
||||||
@@ -59,24 +55,16 @@ test('same with double subs', async () => {
|
|||||||
let priv = generateSecretKey()
|
let priv = generateSecretKey()
|
||||||
let pub = getPublicKey(priv)
|
let pub = getPublicKey(priv)
|
||||||
|
|
||||||
pool.subscribeMany(
|
pool.subscribeMany(relayURLs, [{ authors: [pub] }], {
|
||||||
relayURLs,
|
onevent(event) {
|
||||||
{ authors: [pub] },
|
received.push(event)
|
||||||
{
|
|
||||||
onevent(event) {
|
|
||||||
received.push(event)
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
)
|
})
|
||||||
pool.subscribeMany(
|
pool.subscribeMany(relayURLs, [{ authors: [pub] }], {
|
||||||
relayURLs,
|
onevent(event) {
|
||||||
{ authors: [pub] },
|
received.push(event)
|
||||||
{
|
|
||||||
onevent(event) {
|
|
||||||
received.push(event)
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
)
|
})
|
||||||
|
|
||||||
let received: Event[] = []
|
let received: Event[] = []
|
||||||
|
|
||||||
@@ -180,16 +168,12 @@ test('query a bunch of events and cancel on eose', async () => {
|
|||||||
let events = new Set<string>()
|
let events = new Set<string>()
|
||||||
|
|
||||||
await new Promise<void>(resolve => {
|
await new Promise<void>(resolve => {
|
||||||
pool.subscribeManyEose(
|
pool.subscribeManyEose(relayURLs, [{ kinds: [0, 1, 2, 3, 4, 5, 6], limit: 40 }], {
|
||||||
relayURLs,
|
onevent(event) {
|
||||||
{ kinds: [0, 1, 2, 3, 4, 5, 6], limit: 40 },
|
events.add(event.id)
|
||||||
{
|
|
||||||
onevent(event) {
|
|
||||||
events.add(event.id)
|
|
||||||
},
|
|
||||||
onclose: resolve as any,
|
|
||||||
},
|
},
|
||||||
)
|
onclose: resolve as any,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(events.size).toBeGreaterThan(50)
|
expect(events.size).toBeGreaterThan(50)
|
||||||
@@ -222,151 +206,6 @@ test('get()', async () => {
|
|||||||
expect(event).toHaveProperty('id', ids[0])
|
expect(event).toHaveProperty('id', ids[0])
|
||||||
})
|
})
|
||||||
|
|
||||||
test('ping-pong timeout in pool', async () => {
|
|
||||||
const mockRelay = mockRelays[0]
|
|
||||||
pool = new SimplePool({ enablePing: true })
|
|
||||||
const relay = await pool.ensureRelay(mockRelay.url)
|
|
||||||
relay.pingTimeout = 50
|
|
||||||
relay.pingFrequency = 50
|
|
||||||
|
|
||||||
let closed = false
|
|
||||||
const closedPromise = new Promise<void>(resolve => {
|
|
||||||
relay.onclose = () => {
|
|
||||||
closed = true
|
|
||||||
resolve()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(relay.connected).toBeTrue()
|
|
||||||
|
|
||||||
// wait for the first ping to succeed
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 75))
|
|
||||||
expect(closed).toBeFalse()
|
|
||||||
|
|
||||||
// now make it unresponsive
|
|
||||||
mockRelay.unresponsive = true
|
|
||||||
|
|
||||||
// wait for the second ping to fail
|
|
||||||
await closedPromise
|
|
||||||
|
|
||||||
expect(relay.connected).toBeFalse()
|
|
||||||
expect(closed).toBeTrue()
|
|
||||||
})
|
|
||||||
|
|
||||||
test('reconnect on disconnect in pool', async () => {
|
|
||||||
const mockRelay = mockRelays[0]
|
|
||||||
pool = new SimplePool({ enablePing: true, enableReconnect: true })
|
|
||||||
const relay = await pool.ensureRelay(mockRelay.url)
|
|
||||||
relay.pingTimeout = 50
|
|
||||||
relay.pingFrequency = 50
|
|
||||||
relay.resubscribeBackoff = [50, 100]
|
|
||||||
|
|
||||||
let closes = 0
|
|
||||||
relay.onclose = () => {
|
|
||||||
closes++
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(relay.connected).toBeTrue()
|
|
||||||
|
|
||||||
// wait for the first ping to succeed
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 75))
|
|
||||||
expect(closes).toBe(0)
|
|
||||||
|
|
||||||
// now make it unresponsive
|
|
||||||
mockRelay.unresponsive = true
|
|
||||||
|
|
||||||
// wait for the second ping to fail, which will trigger a close
|
|
||||||
await new Promise(resolve => {
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
if (closes > 0) {
|
|
||||||
clearInterval(interval)
|
|
||||||
resolve(null)
|
|
||||||
}
|
|
||||||
}, 10)
|
|
||||||
})
|
|
||||||
expect(closes).toBe(1)
|
|
||||||
expect(relay.connected).toBeFalse()
|
|
||||||
|
|
||||||
// now make it responsive again
|
|
||||||
mockRelay.unresponsive = false
|
|
||||||
|
|
||||||
// wait for reconnect
|
|
||||||
await new Promise(resolve => {
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
if (relay.connected) {
|
|
||||||
clearInterval(interval)
|
|
||||||
resolve(null)
|
|
||||||
}
|
|
||||||
}, 10)
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(relay.connected).toBeTrue()
|
|
||||||
expect(closes).toBe(1)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('reconnect with filter update in pool', async () => {
|
|
||||||
const mockRelay = mockRelays[0]
|
|
||||||
const newSince = Math.floor(Date.now() / 1000)
|
|
||||||
pool = new SimplePool({
|
|
||||||
enablePing: true,
|
|
||||||
enableReconnect: filters => {
|
|
||||||
return filters.map(f => ({ ...f, since: newSince }))
|
|
||||||
},
|
|
||||||
})
|
|
||||||
const relay = await pool.ensureRelay(mockRelay.url)
|
|
||||||
relay.pingTimeout = 50
|
|
||||||
relay.pingFrequency = 50
|
|
||||||
relay.resubscribeBackoff = [50, 100]
|
|
||||||
|
|
||||||
let closes = 0
|
|
||||||
relay.onclose = () => {
|
|
||||||
closes++
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(relay.connected).toBeTrue()
|
|
||||||
|
|
||||||
const sub = relay.subscribe([{ kinds: [1], since: 0 }], { onevent: () => {} })
|
|
||||||
expect(sub.filters[0].since).toBe(0)
|
|
||||||
|
|
||||||
// wait for the first ping to succeed
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 75))
|
|
||||||
expect(closes).toBe(0)
|
|
||||||
|
|
||||||
// now make it unresponsive
|
|
||||||
mockRelay.unresponsive = true
|
|
||||||
|
|
||||||
// wait for the second ping to fail, which will trigger a close
|
|
||||||
await new Promise(resolve => {
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
if (closes > 0) {
|
|
||||||
clearInterval(interval)
|
|
||||||
resolve(null)
|
|
||||||
}
|
|
||||||
}, 10)
|
|
||||||
})
|
|
||||||
expect(closes).toBe(1)
|
|
||||||
expect(relay.connected).toBeFalse()
|
|
||||||
|
|
||||||
// now make it responsive again
|
|
||||||
mockRelay.unresponsive = false
|
|
||||||
|
|
||||||
// wait for reconnect
|
|
||||||
await new Promise(resolve => {
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
if (relay.connected) {
|
|
||||||
clearInterval(interval)
|
|
||||||
resolve(null)
|
|
||||||
}
|
|
||||||
}, 10)
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(relay.connected).toBeTrue()
|
|
||||||
expect(closes).toBe(1)
|
|
||||||
|
|
||||||
// check if filter was updated
|
|
||||||
expect(sub.filters[0].since).toBe(newSince)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('track relays when publishing', async () => {
|
test('track relays when publishing', async () => {
|
||||||
let event1 = finalizeEvent(
|
let event1 = finalizeEvent(
|
||||||
{
|
{
|
||||||
|
|||||||
6
pool.ts
6
pool.ts
@@ -1,7 +1,7 @@
|
|||||||
/* global WebSocket */
|
/* global WebSocket */
|
||||||
|
|
||||||
import { verifyEvent } from './pure.ts'
|
import { verifyEvent } from './pure.ts'
|
||||||
import { AbstractSimplePool, type AbstractPoolConstructorOptions } from './abstract-pool.ts'
|
import { AbstractSimplePool } from './abstract-pool.ts'
|
||||||
|
|
||||||
var _WebSocket: typeof WebSocket
|
var _WebSocket: typeof WebSocket
|
||||||
|
|
||||||
@@ -14,8 +14,8 @@ export function useWebSocketImplementation(websocketImplementation: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class SimplePool extends AbstractSimplePool {
|
export class SimplePool extends AbstractSimplePool {
|
||||||
constructor(options?: Pick<AbstractPoolConstructorOptions, 'enablePing' | 'enableReconnect'>) {
|
constructor() {
|
||||||
super({ verifyEvent, websocketImplementation: _WebSocket, ...options })
|
super({ verifyEvent, websocketImplementation: _WebSocket })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
282
relay.test.ts
282
relay.test.ts
@@ -117,285 +117,3 @@ test('publish timeout', async () => {
|
|||||||
),
|
),
|
||||||
).rejects.toThrow('publish timed out')
|
).rejects.toThrow('publish timed out')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('ping-pong timeout (with native ping)', async () => {
|
|
||||||
const mockRelay = new MockRelay()
|
|
||||||
let pingCalled = false
|
|
||||||
|
|
||||||
// mock a native ping/pong mechanism
|
|
||||||
;(MockWebSocketClient.prototype as any).ping = function (this: any) {
|
|
||||||
pingCalled = true
|
|
||||||
if (!mockRelay.unresponsive) {
|
|
||||||
this.dispatchEvent(new Event('pong'))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
;(MockWebSocketClient.prototype as any).once = function (
|
|
||||||
this: any,
|
|
||||||
event: string,
|
|
||||||
listener: (...args: any[]) => void,
|
|
||||||
) {
|
|
||||||
if (event === 'pong') {
|
|
||||||
const onceListener = (...args: any[]) => {
|
|
||||||
this.removeEventListener(event, onceListener)
|
|
||||||
listener.apply(this, args)
|
|
||||||
}
|
|
||||||
this.addEventListener('pong', onceListener)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const relay = new Relay(mockRelay.url, { enablePing: true })
|
|
||||||
relay.pingTimeout = 50
|
|
||||||
relay.pingFrequency = 50
|
|
||||||
|
|
||||||
let closed = false
|
|
||||||
const closedPromise = new Promise<void>(resolve => {
|
|
||||||
relay.onclose = () => {
|
|
||||||
closed = true
|
|
||||||
resolve()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
await relay.connect()
|
|
||||||
expect(relay.connected).toBeTrue()
|
|
||||||
|
|
||||||
// wait for the first ping to succeed
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 75))
|
|
||||||
expect(pingCalled).toBeTrue()
|
|
||||||
expect(closed).toBeFalse()
|
|
||||||
|
|
||||||
// now make it unresponsive
|
|
||||||
mockRelay.unresponsive = true
|
|
||||||
|
|
||||||
// wait for the second ping to fail
|
|
||||||
await closedPromise
|
|
||||||
|
|
||||||
expect(relay.connected).toBeFalse()
|
|
||||||
expect(closed).toBeTrue()
|
|
||||||
} finally {
|
|
||||||
delete (MockWebSocketClient.prototype as any).ping
|
|
||||||
delete (MockWebSocketClient.prototype as any).once
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
test('ping-pong timeout (no-ping browser environment)', async () => {
|
|
||||||
// spy on send to ensure the fallback dummy REQ is used, since MockWebSocketClient has no ping
|
|
||||||
const originalSend = MockWebSocketClient.prototype.send
|
|
||||||
let dummyReqSent = false
|
|
||||||
|
|
||||||
try {
|
|
||||||
MockWebSocketClient.prototype.send = function (message: string) {
|
|
||||||
if (message.includes('REQ') && message.includes('a'.repeat(64))) {
|
|
||||||
dummyReqSent = true
|
|
||||||
}
|
|
||||||
originalSend.call(this, message)
|
|
||||||
}
|
|
||||||
|
|
||||||
const mockRelay = new MockRelay()
|
|
||||||
const relay = new Relay(mockRelay.url, { enablePing: true })
|
|
||||||
relay.pingTimeout = 50
|
|
||||||
relay.pingFrequency = 50
|
|
||||||
|
|
||||||
let closed = false
|
|
||||||
const closedPromise = new Promise<void>(resolve => {
|
|
||||||
relay.onclose = () => {
|
|
||||||
closed = true
|
|
||||||
resolve()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
await relay.connect()
|
|
||||||
expect(relay.connected).toBeTrue()
|
|
||||||
|
|
||||||
// wait for the first ping to succeed
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 75))
|
|
||||||
expect(dummyReqSent).toBeTrue()
|
|
||||||
expect(closed).toBeFalse()
|
|
||||||
|
|
||||||
// now make it unresponsive
|
|
||||||
mockRelay.unresponsive = true
|
|
||||||
|
|
||||||
// wait for the second ping to fail
|
|
||||||
await closedPromise
|
|
||||||
|
|
||||||
expect(relay.connected).toBeFalse()
|
|
||||||
expect(closed).toBeTrue()
|
|
||||||
} finally {
|
|
||||||
MockWebSocketClient.prototype.send = originalSend
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
test('ping-pong listeners are cleaned up', async () => {
|
|
||||||
const mockRelay = new MockRelay()
|
|
||||||
let listenerCount = 0
|
|
||||||
|
|
||||||
// mock a native ping/pong mechanism
|
|
||||||
;(MockWebSocketClient.prototype as any).ping = function (this: any) {
|
|
||||||
if (!mockRelay.unresponsive) {
|
|
||||||
this.dispatchEvent(new Event('pong'))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const originalAddEventListener = MockWebSocketClient.prototype.addEventListener
|
|
||||||
MockWebSocketClient.prototype.addEventListener = function (event, listener, options) {
|
|
||||||
if (event === 'pong') {
|
|
||||||
listenerCount++
|
|
||||||
}
|
|
||||||
// @ts-ignore
|
|
||||||
return originalAddEventListener.call(this, event, listener, options)
|
|
||||||
}
|
|
||||||
|
|
||||||
const originalRemoveEventListener = MockWebSocketClient.prototype.removeEventListener
|
|
||||||
MockWebSocketClient.prototype.removeEventListener = function (event, listener) {
|
|
||||||
if (event === 'pong') {
|
|
||||||
listenerCount--
|
|
||||||
}
|
|
||||||
// @ts-ignore
|
|
||||||
return originalRemoveEventListener.call(this, event, listener)
|
|
||||||
}
|
|
||||||
|
|
||||||
// the check in pingpong() is for .once() so we must mock it
|
|
||||||
;(MockWebSocketClient.prototype as any).once = function (
|
|
||||||
this: any,
|
|
||||||
event: string,
|
|
||||||
listener: (...args: any[]) => void,
|
|
||||||
) {
|
|
||||||
const onceListener = (...args: any[]) => {
|
|
||||||
this.removeEventListener(event, onceListener)
|
|
||||||
listener.apply(this, args)
|
|
||||||
}
|
|
||||||
this.addEventListener(event, onceListener)
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const relay = new Relay(mockRelay.url, { enablePing: true })
|
|
||||||
relay.pingTimeout = 50
|
|
||||||
relay.pingFrequency = 50
|
|
||||||
|
|
||||||
await relay.connect()
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 175))
|
|
||||||
|
|
||||||
expect(listenerCount).toBeLessThan(2)
|
|
||||||
|
|
||||||
relay.close()
|
|
||||||
} finally {
|
|
||||||
delete (MockWebSocketClient.prototype as any).ping
|
|
||||||
delete (MockWebSocketClient.prototype as any).once
|
|
||||||
MockWebSocketClient.prototype.addEventListener = originalAddEventListener
|
|
||||||
MockWebSocketClient.prototype.removeEventListener = originalRemoveEventListener
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
test('reconnect on disconnect', async () => {
|
|
||||||
const mockRelay = new MockRelay()
|
|
||||||
const relay = new Relay(mockRelay.url, { enablePing: true, enableReconnect: true })
|
|
||||||
relay.pingTimeout = 50
|
|
||||||
relay.pingFrequency = 50
|
|
||||||
relay.resubscribeBackoff = [50, 100] // short backoff for testing
|
|
||||||
|
|
||||||
let closes = 0
|
|
||||||
relay.onclose = () => {
|
|
||||||
closes++
|
|
||||||
}
|
|
||||||
|
|
||||||
await relay.connect()
|
|
||||||
expect(relay.connected).toBeTrue()
|
|
||||||
|
|
||||||
// wait for the first ping to succeed
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 75))
|
|
||||||
expect(closes).toBe(0)
|
|
||||||
|
|
||||||
// now make it unresponsive
|
|
||||||
mockRelay.unresponsive = true
|
|
||||||
|
|
||||||
// wait for the second ping to fail, which will trigger a close
|
|
||||||
await new Promise(resolve => {
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
if (closes > 0) {
|
|
||||||
clearInterval(interval)
|
|
||||||
resolve(null)
|
|
||||||
}
|
|
||||||
}, 10)
|
|
||||||
})
|
|
||||||
expect(closes).toBe(1)
|
|
||||||
expect(relay.connected).toBeFalse()
|
|
||||||
|
|
||||||
// now make it responsive again
|
|
||||||
mockRelay.unresponsive = false
|
|
||||||
|
|
||||||
// wait for reconnect
|
|
||||||
await new Promise(resolve => {
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
if (relay.connected) {
|
|
||||||
clearInterval(interval)
|
|
||||||
resolve(null)
|
|
||||||
}
|
|
||||||
}, 10)
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(relay.connected).toBeTrue()
|
|
||||||
expect(closes).toBe(1) // should not have closed again
|
|
||||||
})
|
|
||||||
|
|
||||||
test('reconnect with filter update', async () => {
|
|
||||||
const mockRelay = new MockRelay()
|
|
||||||
const newSince = Math.floor(Date.now() / 1000)
|
|
||||||
const relay = new Relay(mockRelay.url, {
|
|
||||||
enablePing: true,
|
|
||||||
enableReconnect: filters => {
|
|
||||||
return filters.map(f => ({ ...f, since: newSince }))
|
|
||||||
},
|
|
||||||
})
|
|
||||||
relay.pingTimeout = 50
|
|
||||||
relay.pingFrequency = 50
|
|
||||||
relay.resubscribeBackoff = [50, 100]
|
|
||||||
|
|
||||||
let closes = 0
|
|
||||||
relay.onclose = () => {
|
|
||||||
closes++
|
|
||||||
}
|
|
||||||
|
|
||||||
await relay.connect()
|
|
||||||
expect(relay.connected).toBeTrue()
|
|
||||||
|
|
||||||
const sub = relay.subscribe([{ kinds: [1], since: 0 }], { onevent: () => {} })
|
|
||||||
expect(sub.filters[0].since).toBe(0)
|
|
||||||
|
|
||||||
// wait for the first ping to succeed
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 75))
|
|
||||||
expect(closes).toBe(0)
|
|
||||||
|
|
||||||
// now make it unresponsive
|
|
||||||
mockRelay.unresponsive = true
|
|
||||||
|
|
||||||
// wait for the second ping to fail, which will trigger a close
|
|
||||||
await new Promise(resolve => {
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
if (closes > 0) {
|
|
||||||
clearInterval(interval)
|
|
||||||
resolve(null)
|
|
||||||
}
|
|
||||||
}, 10)
|
|
||||||
})
|
|
||||||
expect(closes).toBe(1)
|
|
||||||
expect(relay.connected).toBeFalse()
|
|
||||||
|
|
||||||
// now make it responsive again
|
|
||||||
mockRelay.unresponsive = false
|
|
||||||
|
|
||||||
// wait for reconnect
|
|
||||||
await new Promise(resolve => {
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
if (relay.connected) {
|
|
||||||
clearInterval(interval)
|
|
||||||
resolve(null)
|
|
||||||
}
|
|
||||||
}, 10)
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(relay.connected).toBeTrue()
|
|
||||||
expect(closes).toBe(1)
|
|
||||||
|
|
||||||
// check if filter was updated
|
|
||||||
expect(sub.filters[0].since).toBe(newSince)
|
|
||||||
})
|
|
||||||
|
|||||||
25
relay.ts
25
relay.ts
@@ -1,33 +1,20 @@
|
|||||||
/* global WebSocket */
|
/* global WebSocket */
|
||||||
|
|
||||||
import { verifyEvent } from './pure.ts'
|
import { verifyEvent } from './pure.ts'
|
||||||
import { AbstractRelay, type AbstractRelayConstructorOptions } from './abstract-relay.ts'
|
import { AbstractRelay, WebSocketBaseConn } from './abstract-relay.ts'
|
||||||
|
|
||||||
var _WebSocket: typeof WebSocket
|
class BrowserWs extends WebSocket implements WebSocketBase, WebSocketBaseConn {}
|
||||||
|
|
||||||
try {
|
|
||||||
_WebSocket = WebSocket
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
export function useWebSocketImplementation(websocketImplementation: any) {
|
|
||||||
_WebSocket = websocketImplementation
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Relay extends AbstractRelay {
|
export class Relay extends AbstractRelay {
|
||||||
constructor(url: string, options?: Pick<AbstractRelayConstructorOptions, 'enablePing' | 'enableReconnect'>) {
|
constructor(url: string) {
|
||||||
super(url, { verifyEvent, websocketImplementation: _WebSocket, ...options })
|
super(url, { verifyEvent, websocketImplementation: BrowserWs })
|
||||||
}
|
}
|
||||||
|
|
||||||
static async connect(
|
static async connect(url: string): Promise<Relay> {
|
||||||
url: string,
|
const relay = new Relay(url)
|
||||||
options?: Pick<AbstractRelayConstructorOptions, 'enablePing' | 'enableReconnect'>,
|
|
||||||
): Promise<Relay> {
|
|
||||||
const relay = new Relay(url, options)
|
|
||||||
await relay.connect()
|
await relay.connect()
|
||||||
return relay
|
return relay
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RelayRecord = Record<string, { read: boolean; write: boolean }>
|
|
||||||
|
|
||||||
export * from './abstract-relay.ts'
|
export * from './abstract-relay.ts'
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ export class MockRelay {
|
|||||||
public url: string
|
public url: string
|
||||||
public secretKeys: Uint8Array[]
|
public secretKeys: Uint8Array[]
|
||||||
public preloadedEvents: Event[]
|
public preloadedEvents: Event[]
|
||||||
public unresponsive: boolean = false
|
|
||||||
|
|
||||||
constructor(url?: string | undefined) {
|
constructor(url?: string | undefined) {
|
||||||
serial++
|
serial++
|
||||||
@@ -49,7 +48,6 @@ export class MockRelay {
|
|||||||
let subs: { [subId: string]: { conn: any; filters: Filter[] } } = {}
|
let subs: { [subId: string]: { conn: any; filters: Filter[] } } = {}
|
||||||
|
|
||||||
conn.on('message', (message: string) => {
|
conn.on('message', (message: string) => {
|
||||||
if (this.unresponsive) return
|
|
||||||
const data = JSON.parse(message)
|
const data = JSON.parse(message)
|
||||||
|
|
||||||
switch (data[0]) {
|
switch (data[0]) {
|
||||||
|
|||||||
Reference in New Issue
Block a user