Update publish

This commit is contained in:
Jon Staab
2025-04-11 08:03:44 -07:00
parent 989fc74374
commit 837fd1ab66
12 changed files with 219 additions and 274 deletions
+54 -62
View File
@@ -1,11 +1,11 @@
import {describe, expect, it, vi, beforeEach, afterEach} from "vitest"
import {SinglePublish, MultiPublish, PublishEvent} from "../src/publish"
import {publishOne, publish} from "../src/publish"
import {MockAdapter} from "../src/adapter"
import {ClientMessageType} from "../src/message"
import {makeEvent} from "@welshman/util"
import {Nip01Signer} from "@welshman/signer"
describe("SinglePublish", () => {
describe("publishOne", () => {
beforeEach(() => {
vi.useFakeTimers()
})
@@ -19,20 +19,18 @@ describe("SinglePublish", () => {
const adapter = new MockAdapter("1", sendSpy)
const signer = Nip01Signer.ephemeral()
const event = await signer.sign(makeEvent(1))
const pub = new SinglePublish({
relay: "1",
context: {getAdapter: () => adapter},
event,
})
const successSpy = vi.fn()
const failureSpy = vi.fn()
const completeSpy = vi.fn()
pub.on(PublishEvent.Success, successSpy)
pub.on(PublishEvent.Failure, failureSpy)
pub.on(PublishEvent.Complete, completeSpy)
publishOne({
event,
relay: "1",
context: {getAdapter: () => adapter},
onSuccess: successSpy,
onFailure: failureSpy,
onComplete: completeSpy,
})
await vi.advanceTimersByTimeAsync(200)
@@ -42,7 +40,7 @@ describe("SinglePublish", () => {
await vi.runAllTimers()
expect(successSpy).toHaveBeenCalledWith(event.id, "hi")
expect(successSpy).toHaveBeenCalledWith("hi", "1")
expect(failureSpy).not.toHaveBeenCalled()
expect(completeSpy).toHaveBeenCalled()
})
@@ -52,20 +50,18 @@ describe("SinglePublish", () => {
const adapter = new MockAdapter("1", sendSpy)
const signer = Nip01Signer.ephemeral()
const event = await signer.sign(makeEvent(1))
const pub = new SinglePublish({
relay: "1",
context: {getAdapter: () => adapter},
event,
})
const successSpy = vi.fn()
const failureSpy = vi.fn()
const completeSpy = vi.fn()
pub.on(PublishEvent.Success, successSpy)
pub.on(PublishEvent.Failure, failureSpy)
pub.on(PublishEvent.Complete, completeSpy)
publishOne({
event,
relay: "1",
context: {getAdapter: () => adapter},
onSuccess: successSpy,
onFailure: failureSpy,
onComplete: completeSpy,
})
await vi.advanceTimersByTimeAsync(200)
@@ -76,7 +72,7 @@ describe("SinglePublish", () => {
await vi.runAllTimers()
expect(successSpy).not.toHaveBeenCalled()
expect(failureSpy).toHaveBeenCalledWith(event.id, "hi")
expect(failureSpy).toHaveBeenCalledWith("hi", "1")
expect(completeSpy).toHaveBeenCalled()
})
@@ -85,22 +81,20 @@ describe("SinglePublish", () => {
const adapter = new MockAdapter("1", sendSpy)
const signer = Nip01Signer.ephemeral()
const event = await signer.sign(makeEvent(1))
const pub = new SinglePublish({
relay: "1",
context: {getAdapter: () => adapter},
event,
})
const successSpy = vi.fn()
const failureSpy = vi.fn()
const completeSpy = vi.fn()
const timeoutSpy = vi.fn()
pub.on(PublishEvent.Success, successSpy)
pub.on(PublishEvent.Failure, failureSpy)
pub.on(PublishEvent.Complete, completeSpy)
pub.on(PublishEvent.Timeout, timeoutSpy)
publishOne({
event,
relay: "1",
context: {getAdapter: () => adapter},
onSuccess: successSpy,
onFailure: failureSpy,
onComplete: completeSpy,
onTimeout: timeoutSpy,
})
await vi.runAllTimers()
@@ -119,28 +113,28 @@ describe("SinglePublish", () => {
const adapter = new MockAdapter("1", sendSpy)
const signer = Nip01Signer.ephemeral()
const event = await signer.sign(makeEvent(1))
const pub = new SinglePublish({
relay: "1",
context: {getAdapter: () => adapter},
event,
})
const ctrl = new AbortController()
const successSpy = vi.fn()
const failureSpy = vi.fn()
const completeSpy = vi.fn()
const abortSpy = vi.fn()
pub.on(PublishEvent.Success, successSpy)
pub.on(PublishEvent.Failure, failureSpy)
pub.on(PublishEvent.Complete, completeSpy)
pub.on(PublishEvent.Timeout, abortSpy)
publishOne({
event,
relay: "1",
signal: ctrl.signal,
context: {getAdapter: () => adapter},
onSuccess: successSpy,
onFailure: failureSpy,
onComplete: completeSpy,
onTimeout: abortSpy,
})
await vi.runAllTimers()
expect(sendSpy).toHaveBeenCalledWith([ClientMessageType.Event, event])
pub.abort()
ctrl.abort()
await vi.runAllTimers()
@@ -151,7 +145,7 @@ describe("SinglePublish", () => {
})
})
describe("MultiPublish", () => {
describe("publish", () => {
beforeEach(() => {
vi.useFakeTimers()
})
@@ -169,8 +163,12 @@ describe("MultiPublish", () => {
const adapter3 = new MockAdapter("3", send3Spy)
const signer = Nip01Signer.ephemeral()
const event = await signer.sign(makeEvent(1))
const successSpy = vi.fn()
const failureSpy = vi.fn()
const completeSpy = vi.fn()
const timeoutSpy = vi.fn()
const pub = new MultiPublish({
publish({
event,
relays: ["1", "2", "3"],
context: {
@@ -187,25 +185,19 @@ describe("MultiPublish", () => {
}
},
},
onSuccess: successSpy,
onFailure: failureSpy,
onComplete: completeSpy,
onTimeout: timeoutSpy,
})
const successSpy = vi.fn()
const failureSpy = vi.fn()
const completeSpy = vi.fn()
const timeoutSpy = vi.fn()
pub.on(PublishEvent.Success, successSpy)
pub.on(PublishEvent.Failure, failureSpy)
pub.on(PublishEvent.Complete, completeSpy)
pub.on(PublishEvent.Timeout, timeoutSpy)
adapter1.receive(["OK", event.id, true, "hi"])
adapter2.receive(["OK", event.id, false, "hi"])
await vi.runAllTimers()
await vi.runAllTimersAsync()
expect(successSpy).toHaveBeenCalledWith(event.id, "hi", "1")
expect(failureSpy).toHaveBeenCalledWith(event.id, "hi", "2")
expect(successSpy).toHaveBeenCalledWith("hi", "1")
expect(failureSpy).toHaveBeenCalledWith("hi", "2")
expect(completeSpy).toHaveBeenCalledTimes(1)
expect(timeoutSpy).toHaveBeenCalledWith("3")
})
+37 -19
View File
@@ -15,8 +15,14 @@ describe("requestOne", () => {
})
it("everything basically works", async () => {
const sendSpy = vi.fn()
let id
const sendSpy = vi.fn(m => {
if (m[0] === 'REQ') {
id = m[1]
}
})
const adapter = new MockAdapter("1", sendSpy)
const ctrl = new AbortController()
const duplicateSpy = vi.fn()
const invalidSpy = vi.fn()
const filteredSpy = vi.fn()
@@ -28,6 +34,7 @@ describe("requestOne", () => {
relay: "whatever",
filters: [{kinds: [1]}],
context: {getAdapter: () => adapter},
signal: ctrl.signal,
onDuplicate: duplicateSpy,
onInvalid: invalidSpy,
onFiltered: filteredSpy,
@@ -38,7 +45,7 @@ describe("requestOne", () => {
await vi.runAllTimersAsync()
expect(sendSpy).toHaveBeenCalledWith([ClientMessageType.Req, expect.any(String), {kinds: [1]}])
expect(sendSpy).toHaveBeenCalledWith([ClientMessageType.Req, id, {kinds: [1]}])
const signer = Nip01Signer.ephemeral()
const event1 = await signer.sign(makeEvent(1))
@@ -52,17 +59,17 @@ describe("requestOne", () => {
await vi.runAllTimersAsync()
expect(duplicateSpy).toHaveBeenCalledWith(event1)
expect(filteredSpy).toHaveBeenCalledWith(event2)
expect(invalidSpy).toHaveBeenCalledWith(event3)
expect(eventSpy).toHaveBeenCalledWith(event1)
expect(duplicateSpy).toHaveBeenCalledWith(event1, "1")
expect(filteredSpy).toHaveBeenCalledWith(event2, "1")
expect(invalidSpy).toHaveBeenCalledWith(event3, "1")
expect(eventSpy).toHaveBeenCalledWith(event1, "1")
expect(eoseSpy).toHaveBeenCalledTimes(0)
adapter.receive(["EOSE", id])
expect(eoseSpy).toHaveBeenCalledTimes(1)
req.close()
ctrl.abort()
expect(closeSpy).toHaveBeenCalledTimes(1)
})
@@ -78,10 +85,20 @@ describe("request", () => {
})
it("everything basically works", async () => {
const send1Spy = vi.fn()
let id1, id2
const send1Spy = vi.fn(m => {
if (m[0] === 'REQ') {
id1 = m[1]
}
})
const adapter1 = new MockAdapter("1", send1Spy)
const send2Spy = vi.fn()
const send2Spy = vi.fn(m => {
if (m[0] === 'REQ') {
id2 = m[1]
}
})
const adapter2 = new MockAdapter("2", send2Spy)
const ctrl = new AbortController()
const duplicateSpy = vi.fn()
const invalidSpy = vi.fn()
const filteredSpy = vi.fn()
@@ -92,6 +109,7 @@ describe("request", () => {
request({
relays: ["1", "2"],
filters: [{kinds: [1]}],
signal: ctrl.signal,
context: {
getAdapter: (url: string) => (url === "1" ? adapter1 : adapter2),
},
@@ -105,8 +123,8 @@ describe("request", () => {
await vi.runAllTimersAsync()
expect(send1Spy).toHaveBeenCalledTimes(1)
expect(send2Spy).toHaveBeenCalledTimes(1)
expect(send1Spy).toHaveBeenCalledWith([ClientMessageType.Req, id1, {kinds: [1]}])
expect(send2Spy).toHaveBeenCalledWith([ClientMessageType.Req, id2, {kinds: [1]}])
const signer = Nip01Signer.ephemeral()
const event1 = await signer.sign(makeEvent(1))
@@ -114,11 +132,11 @@ describe("request", () => {
const event3 = makeEvent(1)
const event4 = await signer.sign(makeEvent(1))
adapter1.receive(["EVENT", expect.any(String), event1])
adapter1.receive(["EVENT", expect.any(String), event2])
adapter1.receive(["EVENT", expect.any(String), event3])
adapter2.receive(["EVENT", expect.any(String), event1])
adapter2.receive(["EVENT", expect.any(String), event4])
adapter1.receive(["EVENT", id1, event1])
adapter1.receive(["EVENT", id1, event2])
adapter1.receive(["EVENT", id1, event3])
adapter2.receive(["EVENT", id2, event1])
adapter2.receive(["EVENT", id2, event4])
await vi.runAllTimersAsync()
@@ -128,12 +146,12 @@ describe("request", () => {
expect(eventSpy).toHaveBeenCalledWith(event1, "1")
expect(eoseSpy).toHaveBeenCalledTimes(0)
adapter1.receive(["EOSE", expect.any(String)])
adapter2.receive(["EOSE", expect.any(String)])
adapter1.receive(["EOSE", id1])
adapter2.receive(["EOSE", id2])
expect(eoseSpy).toHaveBeenCalledTimes(2)
req.close()
ctrl.abort()
expect(closeSpy).toHaveBeenCalledTimes(1)
})
+2 -4
View File
@@ -11,7 +11,7 @@ import {
import {getAdapter, AdapterContext, AbstractAdapter, AdapterEvent} from "./adapter.js"
import {Negentropy, NegentropyStorageVector} from "./negentropy.js"
import {requestOne} from "./request.js"
import {MultiPublish, PublishEvent} from "./publish.js"
import {publish} from "./publish.js"
export enum DifferenceEvent {
Message = "difference:event:message",
@@ -237,9 +237,7 @@ export const push = async ({context, events, ...options}: PushOptions) => {
const relays = relaysById.get(event.id)
if (relays) {
new Promise<void>(resolve => {
new MultiPublish({event, relays, context}).on(PublishEvent.Complete, resolve)
})
await publish({event, relays, context})
}
}),
)
+78 -115
View File
@@ -12,159 +12,122 @@ export enum PublishStatus {
Aborted = "publish:status:aborted",
}
export enum PublishEvent {
Success = "publish:event:success",
Failure = "publish:event:failure",
Timeout = "publish:event:timeout",
Aborted = "publish:event:aborted",
Complete = "publish:event:complete",
export type PublishResult = {
status: PublishStatus
detail: string
}
// SinglePublish
export type SinglePublishOptions = {
export type PublishOneOptions = {
event: SignedEvent
relay: string
context?: AdapterContext
signal?: AbortSignal
timeout?: number
context?: AdapterContext
onStatus?: (status: PublishStatus, relay: string) => void
onSuccess?: (detail: string, relay: string) => void
onFailure?: (detail: string, relay: string) => void
onTimeout?: (relay: string) => void
onAborted?: (relay: string) => void
onComplete?: () => void
}
export class SinglePublish extends EventEmitter {
status = PublishStatus.Pending
export const publishOne = (options: PublishOneOptions) =>
new Promise(resolve => {
const adapter = getAdapter(options.relay, options.context)
_unsubscriber: () => void
_adapter: AbstractAdapter
let status = PublishStatus.Pending
constructor(readonly options: SinglePublishOptions) {
super()
const setStatus = (_status: PublishStatus) => {
status = _status
options.onStatus?.(status, options.relay)
}
// Set up our adapter
this._adapter = getAdapter(this.options.relay, this.options.context)
const cleanup = () => {
options.onComplete?.()
adapter.cleanup()
resolve(status)
}
// Listen for SinglePublish result
this._unsubscriber = on(
this._adapter,
adapter.on(
AdapterEvent.Receive,
(message: RelayMessage, url: string) => {
if (isRelayOk(message)) {
const [_, id, ok, detail] = message
if (id !== this.options.event.id) return
if (id !== options.event.id) return
if (ok) {
this.status = PublishStatus.Success
this.emit(PublishEvent.Success, id, detail)
setStatus(PublishStatus.Success)
options.onSuccess?.(detail, options.relay)
} else {
this.status = PublishStatus.Failure
this.emit(PublishEvent.Failure, id, detail)
setStatus(PublishStatus.Failure)
options.onFailure?.(detail, options.relay)
}
this.cleanup()
cleanup()
}
},
)
// Set timeout
sleep(this.options.timeout || 10_000).then(() => {
if (this.status === PublishStatus.Pending) {
this.status = PublishStatus.Timeout
this.emit(PublishEvent.Timeout)
options.signal?.addEventListener('abort', () => {
if (status === PublishStatus.Pending) {
setStatus(PublishStatus.Aborted)
options.onAborted?.(options.relay)
}
this.cleanup()
cleanup()
})
// Start asynchronously so the caller can set up listeners
yieldThread().then(() => {
this._adapter.send([ClientMessageType.Event, this.options.event])
})
}
setTimeout(() => {
if (status === PublishStatus.Pending) {
setStatus(PublishStatus.Timeout)
options.onTimeout?.(options.relay)
}
abort = () => {
if (this.status === PublishStatus.Pending) {
this.status = PublishStatus.Aborted
this.emit(PublishEvent.Aborted)
this.cleanup()
}
}
cleanup()
}, options.timeout || 10_000)
cleanup = () => {
this.emit(PublishEvent.Complete)
this.removeAllListeners()
this._adapter.cleanup()
this._unsubscriber()
}
}
adapter.send([ClientMessageType.Event, options.event])
// MultiPublish
setStatus(PublishStatus.Pending)
})
export type MultiPublishOptions = Omit<SinglePublishOptions, "relay"> & {
export type PublishStatusByRelay = Record<string, PublishStatus>
export type PublishOptions = Omit<PublishOneOptions, "relay"> & {
relays: string[]
onUpdate?: (status: PublishStatusByRelay) => void
}
export class MultiPublish extends EventEmitter {
status: Record<string, PublishStatus>
export const publish = async (options: PublishOptions) => {
const status: PublishStatusByRelay = {}
const completed = new Set<string>()
const relays = new Set(options.relays)
_children: SinglePublish[] = []
_completed = new Set<string>()
constructor(options: MultiPublishOptions) {
super()
const relays = new Set(options.relays)
if (relays.size !== options.relays.length) {
console.warn("Non-unique relays passed to MultiPublish")
}
this.status = fromPairs(Array.from(relays).map(relay => [relay, PublishStatus.Pending]))
for (const relay of relays) {
const unicast = new SinglePublish({relay, ...options})
unicast.on(PublishEvent.Success, (id: string, detail: string) => {
this.status[relay] = unicast.status
this.emit(PublishEvent.Success, id, detail, relay)
})
unicast.on(PublishEvent.Failure, (id: string, detail: string) => {
this.status[relay] = unicast.status
this.emit(PublishEvent.Failure, id, detail, relay)
})
unicast.on(PublishEvent.Timeout, () => {
this.status[relay] = unicast.status
this.emit(PublishEvent.Timeout, relay)
})
unicast.on(PublishEvent.Aborted, () => {
this.status[relay] = unicast.status
this.emit(PublishEvent.Aborted, relay)
})
unicast.on(PublishEvent.Complete, () => {
this._completed.add(relay)
this.status[relay] = unicast.status
if (this._completed.size === relays.size) {
this.emit(PublishEvent.Complete)
this.cleanup()
}
})
this._children.push(unicast)
}
if (relays.size !== options.relays.length) {
console.warn("Non-unique relays passed to publish")
}
abort() {
for (const child of this._children) {
child.abort()
}
}
await Promise.all(
options.relays.map(relay =>
publishOne({
relay,
...options,
onStatus: (_status: PublishStatus, relay: string) => {
status[relay] = _status
options.onStatus?.(_status, relay)
options.onUpdate?.(status)
},
onComplete: () => {
completed.add(relay)
cleanup() {
this.removeAllListeners()
}
if (completed.size === relays.size) {
options.onComplete?.()
}
},
})
)
)
return status
}
export const publish = (options: MultiPublishOptions) => new MultiPublish(options)