mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
Expose relay funcs (#31)
This commit is contained in:
committed by
GitHub
parent
ae717a1a4a
commit
1b798b2eee
3
index.d.ts
vendored
3
index.d.ts
vendored
@@ -92,6 +92,9 @@ declare type PoolPublishCallback = (status: number, relay: string) => void;
|
|||||||
declare type RelayPool = {
|
declare type RelayPool = {
|
||||||
setPrivateKey(key: string): void,
|
setPrivateKey(key: string): void,
|
||||||
addRelay(url: string, opts?: RelayPolicy): Relay,
|
addRelay(url: string, opts?: RelayPolicy): Relay,
|
||||||
|
removeRelay(url:string):void,
|
||||||
|
getRelayList():Relay[],
|
||||||
|
relayChangePolicy():Relay,
|
||||||
sub(opts: SubscriptionOptions, id?: string): Subscription,
|
sub(opts: SubscriptionOptions, id?: string): Subscription,
|
||||||
publish(event: Event, cb: PoolPublishCallback): Promise<Event>,
|
publish(event: Event, cb: PoolPublishCallback): Promise<Event>,
|
||||||
close: () => void,
|
close: () => void,
|
||||||
|
|||||||
14
pool.js
14
pool.js
@@ -144,6 +144,20 @@ export function relayPool() {
|
|||||||
relay.close()
|
relay.close()
|
||||||
delete relays[relayURL]
|
delete relays[relayURL]
|
||||||
},
|
},
|
||||||
|
//getRelayList return an array with all the relays stored
|
||||||
|
getRelayList() {
|
||||||
|
return Object.values(relays)
|
||||||
|
},
|
||||||
|
|
||||||
|
relayChangePolicy(url, policy = {read: true, write: true}) {
|
||||||
|
let relayURL = normalizeRelayURL(url)
|
||||||
|
let data = relays[relayURL]
|
||||||
|
if (!data) return
|
||||||
|
|
||||||
|
relays[relayURL].policy = policy
|
||||||
|
|
||||||
|
return relays[relayURL]
|
||||||
|
},
|
||||||
onNotice(cb) {
|
onNotice(cb) {
|
||||||
noticeCallbacks.push(cb)
|
noticeCallbacks.push(cb)
|
||||||
},
|
},
|
||||||
|
|||||||
13
relay.js
13
relay.js
@@ -83,7 +83,7 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
|
|||||||
if (data.length >= 1) {
|
if (data.length >= 1) {
|
||||||
switch (data[0]) {
|
switch (data[0]) {
|
||||||
case 'NOTICE':
|
case 'NOTICE':
|
||||||
if (data.length != 2) {
|
if (data.length !== 2) {
|
||||||
// ignore empty or malformed notice
|
// ignore empty or malformed notice
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -91,7 +91,7 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
|
|||||||
onNotice(data[1])
|
onNotice(data[1])
|
||||||
return
|
return
|
||||||
case 'EOSE':
|
case 'EOSE':
|
||||||
if (data.length != 2) {
|
if (data.length !== 2) {
|
||||||
// ignore malformed EOSE
|
// ignore malformed EOSE
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -101,7 +101,7 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
case 'EVENT':
|
case 'EVENT':
|
||||||
if (data.length != 3) {
|
if (data.length !== 3) {
|
||||||
// ignore malformed EVENT
|
// ignore malformed EVENT
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -114,6 +114,13 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
|
|||||||
eventListeners[channel](event)
|
eventListeners[channel](event)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
||||||
|
case 'OK':
|
||||||
|
if (data.length !== 4) {
|
||||||
|
// ignore malformed EVENT
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user