From 1b798b2eeef376eddcac85ebbf83c24a06538600 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=8D=C3=B1igo=20Ar=C3=A9jula=20A=C3=ADsa?= Date: Sat, 26 Nov 2022 03:21:33 +0100 Subject: [PATCH] Expose relay funcs (#31) --- index.d.ts | 3 +++ pool.js | 14 ++++++++++++++ relay.js | 13 ++++++++++--- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index ab8f861..11b7624 100644 --- a/index.d.ts +++ b/index.d.ts @@ -92,6 +92,9 @@ declare type PoolPublishCallback = (status: number, relay: string) => void; declare type RelayPool = { setPrivateKey(key: string): void, addRelay(url: string, opts?: RelayPolicy): Relay, + removeRelay(url:string):void, + getRelayList():Relay[], + relayChangePolicy():Relay, sub(opts: SubscriptionOptions, id?: string): Subscription, publish(event: Event, cb: PoolPublishCallback): Promise, close: () => void, diff --git a/pool.js b/pool.js index d28e0ae..5e60c28 100644 --- a/pool.js +++ b/pool.js @@ -144,6 +144,20 @@ export function relayPool() { relay.close() 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) { noticeCallbacks.push(cb) }, diff --git a/relay.js b/relay.js index 1c3a75e..7f23c15 100644 --- a/relay.js +++ b/relay.js @@ -83,7 +83,7 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) { if (data.length >= 1) { switch (data[0]) { case 'NOTICE': - if (data.length != 2) { + if (data.length !== 2) { // ignore empty or malformed notice return } @@ -91,7 +91,7 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) { onNotice(data[1]) return case 'EOSE': - if (data.length != 2) { + if (data.length !== 2) { // ignore malformed EOSE return } @@ -101,7 +101,7 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) { } return case 'EVENT': - if (data.length != 3) { + if (data.length !== 3) { // ignore malformed EVENT return } @@ -114,6 +114,13 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) { eventListeners[channel](event) } return + + case 'OK': + if (data.length !== 4) { + // ignore malformed EVENT + return + } + return } } }