Fix subscription id length, allow custom verb in publish in order to support AUTH

This commit is contained in:
Jonathan Staab
2023-04-03 10:08:15 -05:00
parent 11986c79c7
commit 30b6dcd687
3 changed files with 8 additions and 25 deletions
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "paravel", "name": "paravel",
"version": "0.1.12", "version": "0.1.13",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "paravel", "name": "paravel",
"version": "0.1.12", "version": "0.1.13",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"husky": "^8.0.3", "husky": "^8.0.3",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "paravel", "name": "paravel",
"version": "0.1.12", "version": "0.1.13",
"description": "Yet another toolkit for nostr", "description": "Yet another toolkit for nostr",
"repository": { "repository": {
"type": "git", "type": "git",
+5 -22
View File
@@ -1,23 +1,6 @@
import type {EventBus} from './util/EventBus.ts' import type {EventBus} from './util/EventBus.ts'
const createFilterId = filters => const createSubId = prefix => [prefix, Math.random().toString().slice(2, 10)].join('-')
[Math.random().toString().slice(2, 6), filters.map(describeFilter).join(":")].join("-")
const describeFilter = ({kinds = [], ...filter}) => {
const parts = []
parts.push(kinds.join(","))
for (const [key, value] of Object.entries(filter)) {
if (value instanceof Array) {
parts.push(`${key}[${value.length}]`)
} else {
parts.push(key)
}
}
return "(" + parts.join(",") + ")"
}
type Executable = { type Executable = {
bus: EventBus bus: EventBus
@@ -30,7 +13,7 @@ export class Executor {
this.target = target this.target = target
} }
subscribe(filters, {onEvent, onEose}) { subscribe(filters, {onEvent, onEose}) {
const id = createFilterId(filters) const id = createSubId('REQ')
const unsubscribe = this.target.bus.addListeners({ const unsubscribe = this.target.bus.addListeners({
EVENT: (url, subid, e) => subid === id && onEvent?.(url, e), EVENT: (url, subid, e) => subid === id && onEvent?.(url, e),
EOSE: (url, subid) => subid === id && onEose?.(url), EOSE: (url, subid) => subid === id && onEose?.(url),
@@ -46,18 +29,18 @@ export class Executor {
}, },
} }
} }
publish(event, {onOk, onError}) { publish(event, {verb = 'EVENT', onOk, onError}) {
const unsubscribe = this.target.bus.addListeners({ const unsubscribe = this.target.bus.addListeners({
OK: (url, id, ...payload) => id === event.id && onOk(url, id, ...payload), OK: (url, id, ...payload) => id === event.id && onOk(url, id, ...payload),
ERROR: (url, id, ...payload) => id === event.id && onError(url, id, ...payload), ERROR: (url, id, ...payload) => id === event.id && onError(url, id, ...payload),
}) })
this.target.send("EVENT", event) this.target.send(verb, event)
return {unsubscribe} return {unsubscribe}
} }
count(filters, {onCount}) { count(filters, {onCount}) {
const id = createFilterId(filters) const id = createSubId('COUNT')
const unsubscribe = this.target.bus.addListeners({ const unsubscribe = this.target.bus.addListeners({
COUNT: (url, subid, ...payload) => { COUNT: (url, subid, ...payload) => {
if (subid === id) { if (subid === id) {