Handle non-unique relay urls passed to publish/request
This commit is contained in:
@@ -114,7 +114,7 @@ export type Selection = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const makeSelection = (relays: string[], weight = 1): Selection => ({
|
const makeSelection = (relays: string[], weight = 1): Selection => ({
|
||||||
relays: relays.map(normalizeRelayUrl),
|
relays: relays.filter(isRelayUrl).map(normalizeRelayUrl),
|
||||||
weight,
|
weight,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -108,10 +108,16 @@ export class MultiPublish extends EventEmitter {
|
|||||||
_children: SinglePublish[] = []
|
_children: SinglePublish[] = []
|
||||||
_completed = new Set<string>()
|
_completed = new Set<string>()
|
||||||
|
|
||||||
constructor({relays, ...options}: MultiPublishOptions) {
|
constructor(options: MultiPublishOptions) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
this.status = fromPairs(relays.map(relay => [relay, PublishStatus.Pending]))
|
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) {
|
for (const relay of relays) {
|
||||||
const unicast = new SinglePublish({relay, ...options})
|
const unicast = new SinglePublish({relay, ...options})
|
||||||
@@ -140,7 +146,7 @@ export class MultiPublish extends EventEmitter {
|
|||||||
this._completed.add(relay)
|
this._completed.add(relay)
|
||||||
this.status[relay] = unicast.status
|
this.status[relay] = unicast.status
|
||||||
|
|
||||||
if (this._completed.size === relays.length) {
|
if (this._completed.size === relays.size) {
|
||||||
this.emit(PublishEvent.Complete)
|
this.emit(PublishEvent.Complete)
|
||||||
this.cleanup()
|
this.cleanup()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -176,10 +176,15 @@ export class MultiRequest extends EventEmitter {
|
|||||||
_children: SingleRequest[] = []
|
_children: SingleRequest[] = []
|
||||||
_closed = new Set<string>()
|
_closed = new Set<string>()
|
||||||
|
|
||||||
constructor({relays, ...options}: MultiRequestOptions) {
|
constructor(options: MultiRequestOptions) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
const tracker = new Tracker()
|
const tracker = new Tracker()
|
||||||
|
const relays = new Set(options.relays)
|
||||||
|
|
||||||
|
if (relays.size !== options.relays.length) {
|
||||||
|
console.warn("Non-unique relays passed to MultiRequest")
|
||||||
|
}
|
||||||
|
|
||||||
for (const relay of relays) {
|
for (const relay of relays) {
|
||||||
const req = new SingleRequest({relay, tracker, ...options})
|
const req = new SingleRequest({relay, tracker, ...options})
|
||||||
@@ -215,7 +220,7 @@ export class MultiRequest extends EventEmitter {
|
|||||||
req.on(RequestEvent.Close, () => {
|
req.on(RequestEvent.Close, () => {
|
||||||
this._closed.add(relay)
|
this._closed.add(relay)
|
||||||
|
|
||||||
if (this._closed.size === relays.length) {
|
if (this._closed.size === relays.size) {
|
||||||
this.emit(RequestEvent.Close)
|
this.emit(RequestEvent.Close)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user