libp2r2p 0.11.18 → 0.11.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,7 +50,7 @@ export class RelayConnection {
50
50
  #publishes = new Map()
51
51
  #authentications = new Map()
52
52
  #counts = new Map()
53
- #retryAt = 0
53
+ #retryAtByType = new Map()
54
54
 
55
55
  constructor (url, { WebSocket: WebSocketImpl = globalThis.WebSocket } = {}) {
56
56
  this.url = url
@@ -124,12 +124,12 @@ export class RelayConnection {
124
124
 
125
125
  // New work respects relay cooldowns; cleanup uses send() directly. These
126
126
  // waits stay within the caller's existing deadline and are cancellable.
127
- #dispatchWork (send, fail) {
127
+ #dispatchWork (type, send, fail) {
128
128
  let timer = null
129
129
  let cancelled = false
130
130
  const dispatch = () => {
131
131
  if (cancelled) return
132
- const remaining = this.#retryAt - Date.now()
132
+ const remaining = (this.#retryAtByType.get(type) ?? 0) - Date.now()
133
133
  if (remaining > 0) {
134
134
  timer = maybeUnref(setTimeout(dispatch, remaining))
135
135
  return
@@ -140,9 +140,9 @@ export class RelayConnection {
140
140
  return () => { cancelled = true; clearTimeout(timer) }
141
141
  }
142
142
 
143
- #rejection (reason, extra, fallback) {
144
- const error = relayRejectionError(reason, extra, fallback)
145
- this.#retryAt = Math.max(this.#retryAt, error.retryAt || 0)
143
+ #rejection (type, reason, params, fallback) {
144
+ const error = relayRejectionError(reason, params, fallback)
145
+ if (error.retryAt !== undefined) this.#retryAtByType.set(type, Math.max(this.#retryAtByType.get(type) ?? 0, error.retryAt))
146
146
  return error
147
147
  }
148
148
 
@@ -163,7 +163,7 @@ export class RelayConnection {
163
163
  }
164
164
  const subscription = { filters, handlers, close, cancelSend: null }
165
165
  this.#subscriptions.set(id, subscription)
166
- subscription.cancelSend = this.#dispatchWork(() => {
166
+ subscription.cancelSend = this.#dispatchWork('REQ', () => {
167
167
  this.send(JSON.stringify(['REQ', id, ...filters]))
168
168
  sent = true
169
169
  }, error => {
@@ -209,7 +209,7 @@ export class RelayConnection {
209
209
  pending.waiters.add(waiter)
210
210
  signal?.addEventListener('abort', onAbort, { once: true })
211
211
  if (created) {
212
- pending.cancelSend = this.#dispatchWork(() => {
212
+ pending.cancelSend = this.#dispatchWork(type, () => {
213
213
  this.send(JSON.stringify([type, event]))
214
214
  }, error => this.#settleEvent(map, event.id, error))
215
215
  }
@@ -224,7 +224,7 @@ export class RelayConnection {
224
224
  const pending = { ...deferred, signal, onAbort, cancelSend: null }
225
225
  this.#counts.set(id, pending)
226
226
  signal?.addEventListener('abort', onAbort, { once: true })
227
- pending.cancelSend = this.#dispatchWork(() => this.send(JSON.stringify(['COUNT', id, ...filters])), error => {
227
+ pending.cancelSend = this.#dispatchWork('COUNT', () => this.send(JSON.stringify(['COUNT', id, ...filters])), error => {
228
228
  this.#settleCount(id, null, error)
229
229
  })
230
230
  return deferred.promise
@@ -277,7 +277,8 @@ export class RelayConnection {
277
277
  if (data[0] === 'CLOSED') {
278
278
  const id = data[1]
279
279
  const subscription = this.#subscriptions.get(id)
280
- const reason = this.#rejection(data[2], data[3], subscription ? 'SUBSCRIPTION_CLOSED' : 'COUNT_CLOSED')
280
+ if (!subscription && !this.#counts.has(id)) return
281
+ const reason = this.#rejection(subscription ? 'REQ' : 'COUNT', data[2], data[3], subscription ? 'SUBSCRIPTION_CLOSED' : 'COUNT_CLOSED')
281
282
  if (subscription) {
282
283
  this.#subscriptions.delete(id)
283
284
  subscription.cancelSend?.()
@@ -286,9 +287,11 @@ export class RelayConnection {
286
287
  return
287
288
  }
288
289
  if (data[0] === 'OK') {
289
- const reason = data[2] === true ? null : this.#rejection(data[3], data[4], 'EVENT_REJECTED')
290
- this.#settleEvent(this.#publishes, data[1], reason, data[3])
291
- this.#settleEvent(this.#authentications, data[1], reason, data[3])
290
+ for (const [type, pending] of [['EVENT', this.#publishes], ['AUTH', this.#authentications]]) {
291
+ if (!pending.has(data[1])) continue
292
+ const reason = data[2] === true ? null : this.#rejection(type, data[3], data[4], 'EVENT_REJECTED')
293
+ this.#settleEvent(pending, data[1], reason, data[3])
294
+ }
292
295
  return
293
296
  }
294
297
  if (data[0] === 'AUTH' && typeof data[1] === 'string') {