something
This commit is contained in:
parent
de1cf0ed60
commit
719462f041
|
@ -7,9 +7,22 @@ import { Queue, normalizeURL } from './utils.ts'
|
|||
import { makeAuthEvent } from './nip42.ts'
|
||||
import { yieldThread } from './helpers.ts'
|
||||
|
||||
export type RelayRecord = Record<string, { read: boolean; write: boolean }>
|
||||
|
||||
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 = {
|
||||
verifyEvent: Nostr['verifyEvent']
|
||||
websocketImplementation?: typeof WebSocket
|
||||
websocketImplementation?: WebSocketBase
|
||||
}
|
||||
|
||||
export class SendingOnClosedConnection extends Error {
|
||||
|
@ -43,7 +56,7 @@ export class AbstractRelay {
|
|||
private serial: number = 0
|
||||
private verifyEvent: Nostr['verifyEvent']
|
||||
|
||||
private _WebSocket: typeof WebSocket
|
||||
private _WebSocket: WebSocketBase
|
||||
|
||||
constructor(url: string, opts: AbstractRelayConstructorOptions) {
|
||||
this.url = normalizeURL(url)
|
||||
|
|
2
justfile
2
justfile
|
@ -13,7 +13,7 @@ test-only file:
|
|||
|
||||
publish: build
|
||||
# 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
|
||||
git checkout -- package.json
|
||||
|
||||
|
|
|
@ -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'
|
|
@ -241,12 +241,15 @@
|
|||
"@noble/hashes": "1.3.1",
|
||||
"@scure/base": "1.1.1",
|
||||
"@scure/bip32": "1.3.1",
|
||||
"@scure/bip39": "1.2.1",
|
||||
"nostr-wasm": "0.1.0"
|
||||
"@scure/bip39": "1.2.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": ">=5.0.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"nostr-wasm": "0.1.0",
|
||||
"ws": "^8.18.3"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"typescript": {
|
||||
"optional": true
|
||||
|
|
16
relay.ts
16
relay.ts
|
@ -1,21 +1,13 @@
|
|||
/* global WebSocket */
|
||||
|
||||
import { verifyEvent } from './pure.ts'
|
||||
import { AbstractRelay } from './abstract-relay.ts'
|
||||
import { AbstractRelay, WebSocketBaseConn } from './abstract-relay.ts'
|
||||
|
||||
var _WebSocket: typeof WebSocket
|
||||
|
||||
try {
|
||||
_WebSocket = WebSocket
|
||||
} catch {}
|
||||
|
||||
export function useWebSocketImplementation(websocketImplementation: any) {
|
||||
_WebSocket = websocketImplementation
|
||||
}
|
||||
class BrowserWs extends WebSocket implements WebSocketBase, WebSocketBaseConn {}
|
||||
|
||||
export class Relay extends AbstractRelay {
|
||||
constructor(url: string) {
|
||||
super(url, { verifyEvent, websocketImplementation: _WebSocket })
|
||||
super(url, { verifyEvent, websocketImplementation: BrowserWs })
|
||||
}
|
||||
|
||||
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'
|
||||
|
|
Loading…
Reference in New Issue