libp2r2p 0.12.20 → 0.12.22
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/package.json +1 -1
- package/private-messenger/index.js +96 -4
package/package.json
CHANGED
|
@@ -122,6 +122,8 @@ const STORAGE_POLICY_BROADCAST_CHANNEL = 'libp2r2p:private-messenger:storage-pol
|
|
|
122
122
|
const DEFAULT_OFFLINE_SKEW_SECONDS = 30
|
|
123
123
|
const DEFAULT_RELOAD_GAP_DELAY_MS = 500
|
|
124
124
|
const DEFAULT_SEEDER_PRESENCE_INTERVAL_MS = 10 * 60 * 1000
|
|
125
|
+
const SEEDER_PRESENCE_COALESCE_MS = 250
|
|
126
|
+
const SEEDER_PRESENCE_MAX_PENDING = 4096
|
|
125
127
|
const DEFAULT_SEEDER_ONLINE_SECONDS = 20 * 60
|
|
126
128
|
const DEFAULT_MAX_DYNAMIC_RECOVERY_SEEDERS = 8
|
|
127
129
|
const DEFAULT_MESSAGE_QUEUE_MAX_BYTES = 16 * 1024 * 1024 // 16 MiB
|
|
@@ -515,6 +517,8 @@ export class PrivateMessenger {
|
|
|
515
517
|
this.watchRevisionByChannel = new Map()
|
|
516
518
|
this.presenceTimers = new Map()
|
|
517
519
|
this.presenceJobs = new Map()
|
|
520
|
+
this.pendingSeederPresence = new Map()
|
|
521
|
+
this.seederPresenceFlushTimer = null
|
|
518
522
|
this.seedQueueOperationTail = Promise.resolve()
|
|
519
523
|
this.stopRelayListWatcher = null
|
|
520
524
|
this.relayListWatcherPubkey = ''
|
|
@@ -952,6 +956,10 @@ export class PrivateMessenger {
|
|
|
952
956
|
this.channelDebugRefs.delete(pubkey)
|
|
953
957
|
}
|
|
954
958
|
}
|
|
959
|
+
this.discardSeederPresence([
|
|
960
|
+
...removedPubkeys,
|
|
961
|
+
...changed.map(channel => channel.pubkey)
|
|
962
|
+
])
|
|
955
963
|
|
|
956
964
|
const storageSnapshot = await activatePrivateMessengerStorage({
|
|
957
965
|
userPubkey: this.userPubkey,
|
|
@@ -1274,6 +1282,78 @@ export class PrivateMessenger {
|
|
|
1274
1282
|
return true
|
|
1275
1283
|
}
|
|
1276
1284
|
|
|
1285
|
+
seederPresenceKey (channelPubkey, senderPubkey) {
|
|
1286
|
+
return JSON.stringify([channelPubkey, senderPubkey])
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
async receiveSeederPresence (channelPubkey, message) {
|
|
1290
|
+
if (this.closePromise || this.sendingPaused() || message.provenance === 'hearsay' ||
|
|
1291
|
+
!this.desiredChannels.has(channelPubkey)) return
|
|
1292
|
+
if (!this.#admitInboundPeer(channelPubkey, 'yell', message)) return
|
|
1293
|
+
const senderPubkey = message.senderPubkey || message.event?.pubkey
|
|
1294
|
+
if (!senderPubkey) return
|
|
1295
|
+
const channel = this.channels.get(channelPubkey)
|
|
1296
|
+
if (!channel) return
|
|
1297
|
+
if (channel.seeders?.length && !channel.seeders.includes(senderPubkey)) return
|
|
1298
|
+
const at = messageTime(message)
|
|
1299
|
+
const key = this.seederPresenceKey(channelPubkey, senderPubkey)
|
|
1300
|
+
const current = this.pendingSeederPresence.get(key)
|
|
1301
|
+
if (current && current.at >= at) return
|
|
1302
|
+
if (current) this.pendingSeederPresence.delete(key)
|
|
1303
|
+
if (!channel.seeders?.length) {
|
|
1304
|
+
const maxDynamic = Number.isSafeInteger(this.maxDynamicRecoverySeeders) &&
|
|
1305
|
+
this.maxDynamicRecoverySeeders > 0
|
|
1306
|
+
? this.maxDynamicRecoverySeeders
|
|
1307
|
+
: 0
|
|
1308
|
+
if (!maxDynamic) return
|
|
1309
|
+
const channelEntries = [...this.pendingSeederPresence.entries()]
|
|
1310
|
+
.filter(([, pending]) => pending.channelPubkey === channelPubkey)
|
|
1311
|
+
if (channelEntries.length >= maxDynamic) {
|
|
1312
|
+
channelEntries.sort((left, right) => left[1].at - right[1].at)
|
|
1313
|
+
for (const [pendingKey] of channelEntries.slice(
|
|
1314
|
+
0,
|
|
1315
|
+
channelEntries.length - maxDynamic + 1
|
|
1316
|
+
)) {
|
|
1317
|
+
this.pendingSeederPresence.delete(pendingKey)
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
while (this.pendingSeederPresence.size >= SEEDER_PRESENCE_MAX_PENDING) {
|
|
1322
|
+
this.pendingSeederPresence.delete(this.pendingSeederPresence.keys().next().value)
|
|
1323
|
+
}
|
|
1324
|
+
this.pendingSeederPresence.set(key, { channelPubkey, senderPubkey, at })
|
|
1325
|
+
if (this.seederPresenceFlushTimer) return
|
|
1326
|
+
this.seederPresenceFlushTimer = this._setTimeout(() => {
|
|
1327
|
+
this.seederPresenceFlushTimer = null
|
|
1328
|
+
this.flushSeederPresence()
|
|
1329
|
+
}, SEEDER_PRESENCE_COALESCE_MS)
|
|
1330
|
+
this.seederPresenceFlushTimer?.unref?.()
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
flushSeederPresence () {
|
|
1334
|
+
const pending = [...this.pendingSeederPresence.values()]
|
|
1335
|
+
this.pendingSeederPresence.clear()
|
|
1336
|
+
for (const { channelPubkey, senderPubkey, at } of pending) {
|
|
1337
|
+
if (this.closePromise || this.sendingPaused() || !this.desiredChannels.has(channelPubkey)) continue
|
|
1338
|
+
const channel = this.channels.get(channelPubkey)
|
|
1339
|
+
if (!channel) continue
|
|
1340
|
+
const configuredSeeders = channel.seeders || []
|
|
1341
|
+
if (configuredSeeders.length && !configuredSeeders.includes(senderPubkey)) continue
|
|
1342
|
+
this.markSeederActive(channelPubkey, senderPubkey, { announced: true, at })
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
discardSeederPresence (channels) {
|
|
1347
|
+
const channelSet = channels ? new Set(channels) : null
|
|
1348
|
+
for (const [key, pending] of this.pendingSeederPresence) {
|
|
1349
|
+
if (!channelSet || channelSet.has(pending.channelPubkey)) this.pendingSeederPresence.delete(key)
|
|
1350
|
+
}
|
|
1351
|
+
if (!this.pendingSeederPresence.size && this.seederPresenceFlushTimer) {
|
|
1352
|
+
this._clearTimeout(this.seederPresenceFlushTimer)
|
|
1353
|
+
this.seederPresenceFlushTimer = null
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1277
1357
|
contentKeyStatus (contentKeyPubkey) {
|
|
1278
1358
|
if (!contentKeyPubkey) return 'none'
|
|
1279
1359
|
return contentKeyPubkey === this.contentKeyPubkey ? 'known' : 'unknown'
|
|
@@ -1425,7 +1505,9 @@ export class PrivateMessenger {
|
|
|
1425
1505
|
onAsk: message => this.receive(pubkey, () => this.handleAsk(pubkey, message), message, { inboundType: 'ask' }),
|
|
1426
1506
|
onReply: message => this.receive(pubkey, () => this.handleReply(pubkey, message), message, { inboundType: 'reply' }),
|
|
1427
1507
|
onTell: message => this.receive(pubkey, () => this.handleTell(pubkey, message), message, { inboundType: 'tell' }),
|
|
1428
|
-
onYell: message =>
|
|
1508
|
+
onYell: message => messageCode(message) === SEEDER_PRESENCE_CODE
|
|
1509
|
+
? this.receiveSeederPresence(pubkey, message)
|
|
1510
|
+
: this.receive(pubkey, () => this.handleYell(pubkey, message), message, { inboundType: 'yell' }),
|
|
1429
1511
|
onNym: message => this.receive(pubkey, () => this.handleNym(pubkey, message), message, { inboundType: 'nym' }),
|
|
1430
1512
|
onMessage: message => {
|
|
1431
1513
|
const type = eventType(message.event)
|
|
@@ -1939,11 +2021,13 @@ export class PrivateMessenger {
|
|
|
1939
2021
|
}
|
|
1940
2022
|
|
|
1941
2023
|
recordInterruption (channels, at = nowSeconds()) {
|
|
2024
|
+
const now = nowSeconds()
|
|
2025
|
+
const eventTime = Number.isSafeInteger(at) && at >= 0 ? Math.min(at, now) : now
|
|
1942
2026
|
const state = this.readState()
|
|
1943
2027
|
for (const pubkey of channels) {
|
|
1944
2028
|
if (!this.offlineRecoverySecondsFor(pubkey)) continue
|
|
1945
2029
|
const current = state.channels[pubkey] || {}
|
|
1946
|
-
const start = Math.max(0,
|
|
2030
|
+
const start = Math.max(0, eventTime - this.offlineSkewSeconds)
|
|
1947
2031
|
current.openOfflineStart = Math.min(current.openOfflineStart ?? start, start)
|
|
1948
2032
|
state.channels[pubkey] = current
|
|
1949
2033
|
}
|
|
@@ -1970,6 +2054,7 @@ export class PrivateMessenger {
|
|
|
1970
2054
|
|
|
1971
2055
|
unwatch (channels) {
|
|
1972
2056
|
const pubkeys = channels ? uniq(Array.isArray(channels) ? channels : [channels]) : [...this.desiredChannels]
|
|
2057
|
+
this.discardSeederPresence(pubkeys)
|
|
1973
2058
|
for (const extension of this.extensions) extension.unwatch?.(pubkeys)
|
|
1974
2059
|
for (const pubkey of pubkeys) {
|
|
1975
2060
|
this.desiredChannels.delete(pubkey)
|
|
@@ -2097,6 +2182,7 @@ export class PrivateMessenger {
|
|
|
2097
2182
|
applyPause (reason) {
|
|
2098
2183
|
if (typeof reason !== 'string' || !reason.trim()) throw new ValidationError('PAUSE_REASON_REQUIRED')
|
|
2099
2184
|
this.assertOpen()
|
|
2185
|
+
if (reason !== 'capacity') this.discardSeederPresence()
|
|
2100
2186
|
this.pauseReasons.add(reason)
|
|
2101
2187
|
this.pauseRevisions.set(reason, (this.pauseRevisions.get(reason) || 0) + 1)
|
|
2102
2188
|
const pausedError = this.pausedError()
|
|
@@ -2513,7 +2599,10 @@ export class PrivateMessenger {
|
|
|
2513
2599
|
if (this.pauseReasons.has('capacity')) throw this.pausedError()
|
|
2514
2600
|
const capacity = await this.queue.getCapacity()
|
|
2515
2601
|
if (capacity.usedBytes >= inboxCapacityLimits(capacity.maxBytes).highWaterBytes) {
|
|
2516
|
-
throw await this.beginCapacityPressure(capacity
|
|
2602
|
+
throw await this.beginCapacityPressure(capacity, {
|
|
2603
|
+
channelPubkey,
|
|
2604
|
+
interruptionAt: messageTime(message)
|
|
2605
|
+
})
|
|
2517
2606
|
}
|
|
2518
2607
|
const record = {
|
|
2519
2608
|
type,
|
|
@@ -2782,7 +2871,8 @@ export class PrivateMessenger {
|
|
|
2782
2871
|
}
|
|
2783
2872
|
}
|
|
2784
2873
|
|
|
2785
|
-
async beginCapacityPressure (capacity) {
|
|
2874
|
+
async beginCapacityPressure (capacity, { channelPubkey, interruptionAt = nowSeconds() } = {}) {
|
|
2875
|
+
if (channelPubkey) this.recordInterruption([channelPubkey], interruptionAt)
|
|
2786
2876
|
this.applyCapacityRecoveryTarget(capacity)
|
|
2787
2877
|
const pause = this.pauseReasons.has('capacity') ? null : this.pause('capacity')
|
|
2788
2878
|
pause?.catch(error => this.onError?.(error))
|
|
@@ -4178,6 +4268,7 @@ export class PrivateMessenger {
|
|
|
4178
4268
|
await (this.seedStorage ? this.seedStorage.removeLocal({ channelPubkey: pubkey }) : this.seedQueue?.removeBy('byChannel', pubkey))
|
|
4179
4269
|
}
|
|
4180
4270
|
for (const pubkey of stalePubkeys) this.dropRecoveryState(pubkey)
|
|
4271
|
+
this.discardSeederPresence(stalePubkeys)
|
|
4181
4272
|
this.state = state
|
|
4182
4273
|
if (stalePubkeys.length) await this.removeChannelStates(stalePubkeys)
|
|
4183
4274
|
})
|
|
@@ -4215,6 +4306,7 @@ export class PrivateMessenger {
|
|
|
4215
4306
|
this.decryptionScheduler.close()
|
|
4216
4307
|
this.stopDecryptionForeground?.()
|
|
4217
4308
|
if (this.closePromise) return this.closePromise
|
|
4309
|
+
this.discardSeederPresence()
|
|
4218
4310
|
const initSettledPromise = this.initSettledPromise
|
|
4219
4311
|
this.sendRoutingLifetime.abort()
|
|
4220
4312
|
this.publicationRelayHealth.clear()
|