mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
EOSE nip-15
This commit is contained in:
7
pool.js
7
pool.js
@@ -26,7 +26,7 @@ export function relayPool() {
|
|||||||
|
|
||||||
const activeSubscriptions = {}
|
const activeSubscriptions = {}
|
||||||
|
|
||||||
const sub = ({cb, filter, beforeSend}, id) => {
|
const sub = ({cb, filter, beforeSend}, id, cbEose) => {
|
||||||
if (!id) id = Math.random().toString().slice(2)
|
if (!id) id = Math.random().toString().slice(2)
|
||||||
|
|
||||||
const subControllers = Object.fromEntries(
|
const subControllers = Object.fromEntries(
|
||||||
@@ -34,7 +34,8 @@ export function relayPool() {
|
|||||||
.filter(({policy}) => policy.read)
|
.filter(({policy}) => policy.read)
|
||||||
.map(({relay}) => [
|
.map(({relay}) => [
|
||||||
relay.url,
|
relay.url,
|
||||||
relay.sub({cb: event => cb(event, relay.url), filter, beforeSend}, id)
|
relay.sub({cb: event => cb(event, relay.url), filter, beforeSend}, id,
|
||||||
|
cbEose)
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -60,7 +61,7 @@ export function relayPool() {
|
|||||||
const addRelay = relay => {
|
const addRelay = relay => {
|
||||||
subControllers[relay.url] = relay.sub(
|
subControllers[relay.url] = relay.sub(
|
||||||
{cb: event => cb(event, relay.url), filter, beforeSend},
|
{cb: event => cb(event, relay.url), filter, beforeSend},
|
||||||
id
|
id, () => cbEose(relay.url)
|
||||||
)
|
)
|
||||||
return activeSubscriptions[id]
|
return activeSubscriptions[id]
|
||||||
}
|
}
|
||||||
|
|||||||
75
relay.js
75
relay.js
@@ -28,7 +28,8 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var channels = {}
|
var eventListeners = {}
|
||||||
|
var eoseListeners = {}
|
||||||
|
|
||||||
function connect() {
|
function connect() {
|
||||||
ws = new WebSocket(url)
|
ws = new WebSocket(url)
|
||||||
@@ -42,8 +43,9 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
|
|||||||
wasClosed = false
|
wasClosed = false
|
||||||
for (let channel in openSubs) {
|
for (let channel in openSubs) {
|
||||||
let filters = openSubs[channel]
|
let filters = openSubs[channel]
|
||||||
let cb = channels[channel]
|
let eventCb = eventListeners[channel]
|
||||||
sub({cb, filter: filters}, channel)
|
let eoseCb = eoseListeners[channel]
|
||||||
|
sub({eventCb, filter: filters}, channel, eoseCb)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,30 +80,40 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
|
|||||||
data = e.data
|
data = e.data
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.length > 1) {
|
if (data.length >= 1) {
|
||||||
if (data[0] === 'NOTICE') {
|
switch (data[0]) {
|
||||||
if (data.length < 2) return
|
case 'NOTICE':
|
||||||
|
if (data.length != 2) {
|
||||||
console.log('message from relay ' + url + ': ' + data[1])
|
// ignore empty or malformed notice
|
||||||
onNotice(data[1])
|
return
|
||||||
return
|
}
|
||||||
}
|
console.log(`message from relay ${url}: ${data[1]}`)
|
||||||
|
onNotice(data[1])
|
||||||
if (data[0] === 'EVENT') {
|
return
|
||||||
if (data.length < 3) return
|
case 'EOSE':
|
||||||
|
if (data.length != 2) {
|
||||||
let channel = data[1]
|
// ignore malformed EOSE
|
||||||
let event = data[2]
|
return
|
||||||
|
}
|
||||||
if (
|
console.log(`Channel ${data[1]}: End-of-stored-events`)
|
||||||
validateEvent(event) &&
|
if (eoseListeners[data[1]]) {
|
||||||
(isSetToSkipVerification[channel] || verifySignature(event)) &&
|
eoseListeners[data[1]]()
|
||||||
channels[channel] &&
|
}
|
||||||
matchFilters(openSubs[channel], event)
|
return
|
||||||
) {
|
case 'EVENT':
|
||||||
channels[channel](event)
|
if (data.length != 3) {
|
||||||
}
|
// ignore malformed EVENT
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
let channel = data[1]
|
||||||
|
let event = data[2]
|
||||||
|
if (validateEvent(event) &&
|
||||||
|
(isSetToSkipVerification[channel] || verifySignature(event)) &&
|
||||||
|
eventListeners[channel] &&
|
||||||
|
matchFilters(openSubs[channel], event)) {
|
||||||
|
eventListeners[channel](event)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,7 +134,8 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
|
|||||||
|
|
||||||
const sub = (
|
const sub = (
|
||||||
{cb, filter, beforeSend, skipVerification},
|
{cb, filter, beforeSend, skipVerification},
|
||||||
channel = Math.random().toString().slice(2)
|
channel = Math.random().toString().slice(2),
|
||||||
|
eoseCb
|
||||||
) => {
|
) => {
|
||||||
var filters = []
|
var filters = []
|
||||||
if (Array.isArray(filter)) {
|
if (Array.isArray(filter)) {
|
||||||
@@ -137,7 +150,8 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
trySend(['REQ', channel, ...filters])
|
trySend(['REQ', channel, ...filters])
|
||||||
channels[channel] = cb
|
eventListeners[channel] = cb
|
||||||
|
eoseListeners[channel] = eoseCb
|
||||||
openSubs[channel] = filters
|
openSubs[channel] = filters
|
||||||
isSetToSkipVerification[channel] = skipVerification
|
isSetToSkipVerification[channel] = skipVerification
|
||||||
|
|
||||||
@@ -153,7 +167,8 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
|
|||||||
}) => sub({cb, filter, beforeSend, skipVerification}, channel),
|
}) => sub({cb, filter, beforeSend, skipVerification}, channel),
|
||||||
unsub: () => {
|
unsub: () => {
|
||||||
delete openSubs[channel]
|
delete openSubs[channel]
|
||||||
delete channels[channel]
|
delete eventListeners[channel]
|
||||||
|
delete eoseListeners[channel]
|
||||||
delete isSetToSkipVerification[channel]
|
delete isSetToSkipVerification[channel]
|
||||||
trySend(['CLOSE', channel])
|
trySend(['CLOSE', channel])
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user