mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-10 09:08:50 +00:00
Compare commits
1 Commits
v2.15.2
...
node-webso
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
719462f041 |
@@ -7,14 +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
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SendingOnClosedConnection extends Error {
|
export class SendingOnClosedConnection extends Error {
|
||||||
@@ -40,7 +48,7 @@ export class AbstractRelay {
|
|||||||
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
|
||||||
@@ -48,15 +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.pingHeartBeat = opts.pingHeartBeat
|
|
||||||
// this.pingFrequency = opts.pingFrequency
|
|
||||||
// this.pingTimeout = opts.pingTimeout
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async connect(url: string, opts: AbstractRelayConstructorOptions): Promise<AbstractRelay> {
|
static async connect(url: string, opts: AbstractRelayConstructorOptions): Promise<AbstractRelay> {
|
||||||
@@ -110,10 +115,6 @@ export class AbstractRelay {
|
|||||||
this.ws.onopen = () => {
|
this.ws.onopen = () => {
|
||||||
clearTimeout(this.connectionTimeoutHandle)
|
clearTimeout(this.connectionTimeoutHandle)
|
||||||
this._connected = true
|
this._connected = true
|
||||||
if (this.ws && this.ws.ping) {
|
|
||||||
// && this.pingHeartBeat
|
|
||||||
this.pingpong()
|
|
||||||
}
|
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,35 +146,6 @@ export class AbstractRelay {
|
|||||||
return this.connectionPromise
|
return this.connectionPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
private async receivePong() {
|
|
||||||
return new Promise((res, err) => {
|
|
||||||
;(this.ws && this.ws.on && this.ws.on('pong', () => res(true))) || err("ws can't listen for pong")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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) {
|
|
||||||
// send a ping
|
|
||||||
this.ws && this.ws.ping && this.ws.ping()
|
|
||||||
// wait for either a pong or a timeout
|
|
||||||
const result = await Promise.any([
|
|
||||||
this.receivePong(),
|
|
||||||
new Promise(res => setTimeout(() => res(false), 10000)), // TODO: opts.pingTimeout
|
|
||||||
])
|
|
||||||
console.error('pingpong result', result)
|
|
||||||
if (result) {
|
|
||||||
// schedule another pingpong
|
|
||||||
setTimeout(() => this.pingpong(), 10000) // TODO: opts.pingFrequency
|
|
||||||
} else {
|
|
||||||
// pingpong closing socket
|
|
||||||
this.ws && this.ws.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async runQueue() {
|
private async runQueue() {
|
||||||
this.queueRunning = true
|
this.queueRunning = true
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|||||||
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.15.2",
|
"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
|
||||||
|
|
||||||
|
|||||||
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'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
8
nip46.ts
8
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
|
||||||
@@ -237,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.
|
||||||
|
|||||||
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.15.2",
|
"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
|
||||||
|
|||||||
16
relay.ts
16
relay.ts
@@ -1,21 +1,13 @@
|
|||||||
/* global WebSocket */
|
/* global WebSocket */
|
||||||
|
|
||||||
import { verifyEvent } from './pure.ts'
|
import { verifyEvent } from './pure.ts'
|
||||||
import { AbstractRelay } 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) {
|
constructor(url: string) {
|
||||||
super(url, { verifyEvent, websocketImplementation: _WebSocket })
|
super(url, { verifyEvent, websocketImplementation: BrowserWs })
|
||||||
}
|
}
|
||||||
|
|
||||||
static async connect(url: string): Promise<Relay> {
|
static async connect(url: string): Promise<Relay> {
|
||||||
@@ -25,6 +17,4 @@ export class Relay extends AbstractRelay {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RelayRecord = Record<string, { read: boolean; write: boolean }>
|
|
||||||
|
|
||||||
export * from './abstract-relay.ts'
|
export * from './abstract-relay.ts'
|
||||||
|
|||||||
Reference in New Issue
Block a user