libp2r2p 0.12.2 → 0.12.3

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.
package/README.md CHANGED
@@ -296,6 +296,11 @@ Web Lock when available; waits hold neither that lock nor the normal inbox/IDB
296
296
  queues. Fragment assembly survives scheduling boundaries. Pause/close cancel
297
297
  waits; channel removal fences its queued and in-flight results.
298
298
 
299
+ `PRIVATE_MESSENGER_PAUSED` errors include the stable `code` and a copied
300
+ `pauseReasons` array. `QUEUE_CAPACITY_EXCEEDED` errors identify the affected
301
+ `inbox` or `seeds` queue and include bounded `requiredBytes`, `usedBytes` and
302
+ `maxBytes` diagnostics. These fields contain no messages or ciphertext.
303
+
299
304
  Private-channel consumers may opt in
300
305
  with `onDecryptionFailure({ error, record })`, returning true only after durable
301
306
  handling, and replay records with `replayDeferredDecryptions`. Without the hook,
@@ -54,6 +54,15 @@ function byteLength (value) {
54
54
  return encoder.encode(String(value)).length
55
55
  }
56
56
 
57
+ function capacityError (requiredBytes, usedBytes, maxBytes) {
58
+ return Object.assign(new Error('QUEUE_CAPACITY_EXCEEDED'), {
59
+ code: 'QUEUE_CAPACITY_EXCEEDED',
60
+ requiredBytes,
61
+ usedBytes,
62
+ maxBytes
63
+ })
64
+ }
65
+
57
66
  function normalizeEvictionPolicy (policy) {
58
67
  if (policy === 'opposite-end' || policy === undefined || policy === null) return 'opposite-end'
59
68
  if (policy === 'reject') return 'reject'
@@ -471,7 +480,7 @@ export async function createQueue ({
471
480
  if (requiredBytes > sessionMaxBytes) throw new Error('QUEUE_ITEM_TOO_LARGE')
472
481
  if (configuredEvictionPolicy === 'reject') {
473
482
  if (state.usedBytes + requiredBytes > sessionMaxBytes) {
474
- throw Object.assign(new Error('QUEUE_CAPACITY_EXCEEDED'), { requiredBytes, maxBytes: sessionMaxBytes })
483
+ throw capacityError(requiredBytes, state.usedBytes, sessionMaxBytes)
475
484
  }
476
485
  return
477
486
  }
@@ -480,7 +489,7 @@ export async function createQueue ({
480
489
  while (state.usedBytes + requiredBytes > targetBytes) {
481
490
  if (!await evictOne(tx, state, options)) break
482
491
  }
483
- if (state.usedBytes + requiredBytes > sessionMaxBytes) throw new Error('QUEUE_CAPACITY_EXCEEDED')
492
+ if (state.usedBytes + requiredBytes > sessionMaxBytes) throw capacityError(requiredBytes, state.usedBytes, sessionMaxBytes)
484
493
  }
485
494
 
486
495
  async function evictToBytes (tx, state, targetBytes, options = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libp2r2p",
3
- "version": "0.12.2",
3
+ "version": "0.12.3",
4
4
  "description": "Peer-to-relay-to-peer",
5
5
  "keywords": [
6
6
  "p2r2p",
@@ -133,6 +133,30 @@ const SEED_QUEUE_INDEXES = {
133
133
  byChannelTime: ['channelPubkey', SEED_TIME],
134
134
  byTime: SEED_TIME
135
135
  }
136
+
137
+ function isQueueCapacityError (error) {
138
+ return error?.code === 'QUEUE_CAPACITY_EXCEEDED' || error?.message === 'QUEUE_CAPACITY_EXCEEDED'
139
+ }
140
+
141
+ async function annotateQueueCapacityError (error, queue, getCapacity) {
142
+ if (!isQueueCapacityError(error)) return error
143
+ let capacity = {}
144
+ try {
145
+ capacity = await getCapacity?.() || {}
146
+ } catch {
147
+ // Preserve the original queue failure even if a follow-up snapshot fails.
148
+ }
149
+ const value = key => Number.isSafeInteger(error?.[key])
150
+ ? error[key]
151
+ : Number.isSafeInteger(capacity?.[key]) ? capacity[key] : undefined
152
+ return Object.assign(error, {
153
+ code: 'QUEUE_CAPACITY_EXCEEDED',
154
+ queue,
155
+ ...(value('requiredBytes') == null ? {} : { requiredBytes: value('requiredBytes') }),
156
+ ...(value('usedBytes') == null ? {} : { usedBytes: value('usedBytes') }),
157
+ ...(value('maxBytes') == null ? {} : { maxBytes: value('maxBytes') })
158
+ })
159
+ }
136
160
  const encoder = new TextEncoder()
137
161
  const noContentKeys = async () => ({})
138
162
 
@@ -1788,7 +1812,10 @@ export class PrivateMessenger {
1788
1812
  }
1789
1813
 
1790
1814
  pausedError () {
1791
- return Object.assign(new Error('PRIVATE_MESSENGER_PAUSED'), { pauseReasons: this.readStatus().pauseReasons })
1815
+ return Object.assign(new Error('PRIVATE_MESSENGER_PAUSED'), {
1816
+ code: 'PRIVATE_MESSENGER_PAUSED',
1817
+ pauseReasons: this.readStatus().pauseReasons
1818
+ })
1792
1819
  }
1793
1820
 
1794
1821
  recoveryTask (attempt, { network = false, retryable = isRecoverableStorageFailure } = {}) {
@@ -2124,15 +2151,20 @@ export class PrivateMessenger {
2124
2151
  }
2125
2152
  this.recordInterruption([channelPubkey], message.outer?.created_at || message.event?.created_at || nowSeconds())
2126
2153
  // Do not await stop from inside its own transport callback.
2127
- const atCapacity = err.message === 'QUEUE_CAPACITY_EXCEEDED'
2154
+ const atCapacity = isQueueCapacityError(err)
2128
2155
  if (!atCapacity && isRecoverableStorageFailure(err)) this.pendingStorageWrites.set(JSON.stringify(dedupeKey || [channelPubkey, type]), { record, dedupeKey, at: message.outer?.created_at || message.event?.created_at })
2129
- ;(atCapacity ? this.pause('capacity') : isRecoverableStorageFailure(err) ? this.pauseInternally('storage') : this.pause('storage')).catch(error => this.onError?.(error))
2130
2156
  if (atCapacity) {
2157
+ await annotateQueueCapacityError(err, 'inbox', () => this.queue.getCapacity())
2158
+ const pause = this.pause('capacity')
2159
+ Object.assign(err, { pauseReasons: this.readStatus().pauseReasons })
2160
+ pause.catch(error => this.onError?.(error))
2131
2161
  this.capacityRequiredBytes = Math.max(this.capacityRequiredBytes, err.requiredBytes || 0)
2132
2162
  if (!this.capacityTimer) {
2133
2163
  this.capacityTimer = setInterval(() => this.checkCapacity().catch(error => this.onError?.(error)), 1000)
2134
2164
  this.capacityTimer.unref?.()
2135
2165
  }
2166
+ } else {
2167
+ ;(isRecoverableStorageFailure(err) ? this.pauseInternally('storage') : this.pause('storage')).catch(error => this.onError?.(error))
2136
2168
  }
2137
2169
  await this.flushStateWrites()
2138
2170
  throw err
@@ -2165,7 +2197,12 @@ export class PrivateMessenger {
2165
2197
  }
2166
2198
  return
2167
2199
  }
2168
- return this.runSeedQueueOperation(() => this.enqueueQueueSeed(channelPubkey, seed))
2200
+ try {
2201
+ return await this.runSeedQueueOperation(() => this.enqueueQueueSeed(channelPubkey, seed))
2202
+ } catch (error) {
2203
+ await annotateQueueCapacityError(error, 'seeds', () => this.seedQueue?.getCapacity?.())
2204
+ throw error
2205
+ }
2169
2206
  }
2170
2207
 
2171
2208
  async enqueueQueueSeed (channelPubkey, seed) {
@@ -38,6 +38,15 @@ export function createQueue ({
38
38
  return encoder.encode(String(value)).length
39
39
  }
40
40
 
41
+ function capacityError (requiredBytes, usedBytes, maxBytes) {
42
+ return Object.assign(new Error('QUEUE_CAPACITY_EXCEEDED'), {
43
+ code: 'QUEUE_CAPACITY_EXCEEDED',
44
+ requiredBytes,
45
+ usedBytes,
46
+ maxBytes
47
+ })
48
+ }
49
+
41
50
  function hasByteLimit () {
42
51
  return Number.isFinite(sessionMaxBytes)
43
52
  }
@@ -222,7 +231,7 @@ export function createQueue ({
222
231
  : evictOneFromHead(state, options)
223
232
  if (!evicted) break
224
233
  }
225
- if (state.usedBytes + requiredBytes > sessionMaxBytes) throw new Error('QUEUE_CAPACITY_EXCEEDED')
234
+ if (state.usedBytes + requiredBytes > sessionMaxBytes) throw capacityError(requiredBytes, state.usedBytes, sessionMaxBytes)
226
235
  return state
227
236
  }
228
237
 
@@ -291,7 +300,7 @@ export function createQueue ({
291
300
  if (hasByteLimit() && stored.byteSize > sessionMaxBytes) throw new Error('QUEUE_ITEM_TOO_LARGE')
292
301
  if (delta > 0) {
293
302
  if (options.evict === false) {
294
- if (hasByteLimit() && state.usedBytes + delta > sessionMaxBytes) throw new Error('QUEUE_CAPACITY_EXCEEDED')
303
+ if (hasByteLimit() && state.usedBytes + delta > sessionMaxBytes) throw capacityError(delta, state.usedBytes, sessionMaxBytes)
295
304
  } else {
296
305
  evictToFit(state, delta, options)
297
306
  }