libp2r2p 0.12.13 → 0.12.14
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 +16 -0
- package/package.json +1 -1
- package/private-messenger/index.js +87 -14
package/README.md
CHANGED
|
@@ -400,6 +400,22 @@ Coalescing does not provide an ingress rate limit or per-peer quota. Exact
|
|
|
400
400
|
event deduplication, authorization, quarantine, global inbox capacity,
|
|
401
401
|
pause/ACK/NACK ownership and existing storage policy remain independent.
|
|
402
402
|
|
|
403
|
+
### Inbound peer admission
|
|
404
|
+
|
|
405
|
+
`PrivateMessenger` accepts an optional synchronous
|
|
406
|
+
`admitInboundPeer(message, context)` callback. It runs after decryption and
|
|
407
|
+
lifecycle gates, but before typed handlers, automatic replies, seed ingestion
|
|
408
|
+
and inbox admission. The context contains `type`, `channelPubkey`,
|
|
409
|
+
`senderPubkey`, `code`, `bytes` and `now`.
|
|
410
|
+
|
|
411
|
+
Returning `false` drops that delivery locally without an operational error or
|
|
412
|
+
NACK. A thrown error, Promise or non-boolean result fails open: the message is
|
|
413
|
+
admitted and the error is reported. The hook is disabled by default and does
|
|
414
|
+
not coalesce, authorize or persist limits itself.
|
|
415
|
+
|
|
416
|
+
Direct ask/reply/tell/yell events invoke their typed callback once and do not
|
|
417
|
+
invoke `onMessage` again for admission purposes.
|
|
418
|
+
|
|
403
419
|
### Deleting Private Broadcasts
|
|
404
420
|
|
|
405
421
|
By default, each high-level private-message send creates one fresh deletion
|
package/package.json
CHANGED
|
@@ -318,6 +318,7 @@ export class PrivateMessenger {
|
|
|
318
318
|
onDebug,
|
|
319
319
|
onError = defaultOnError,
|
|
320
320
|
resolveInboundCoalescing,
|
|
321
|
+
admitInboundPeer,
|
|
321
322
|
_privateMessage = privateMessage,
|
|
322
323
|
_privateChannel = privateChannel,
|
|
323
324
|
_getRelaysByPubkey = getRelaysByPubkey,
|
|
@@ -342,6 +343,9 @@ export class PrivateMessenger {
|
|
|
342
343
|
if (resolveInboundCoalescing != null && typeof resolveInboundCoalescing !== 'function') {
|
|
343
344
|
throw new ValidationError('INVALID_RESOLVE_INBOUND_COALESCING')
|
|
344
345
|
}
|
|
346
|
+
if (admitInboundPeer != null && typeof admitInboundPeer !== 'function') {
|
|
347
|
+
throw new ValidationError('INVALID_ADMIT_INBOUND_PEER')
|
|
348
|
+
}
|
|
345
349
|
this.fallbackRelays = normalizeFallbackRelays(fallbackRelays)
|
|
346
350
|
this.fallbackDelayMs = normalizeFallbackDelay(fallbackDelayMs)
|
|
347
351
|
this.offlineRecoverySeconds = normalizeOfflineRecoverySeconds(offlineRecoverySeconds)
|
|
@@ -387,6 +391,7 @@ export class PrivateMessenger {
|
|
|
387
391
|
this.onMessageQueued = onMessageQueued
|
|
388
392
|
this.onDebug = onDebug
|
|
389
393
|
this.resolveInboundCoalescing = resolveInboundCoalescing
|
|
394
|
+
this.admitInboundPeer = admitInboundPeer
|
|
390
395
|
this._privateMessage = _privateMessage.createPrivateMessageSession?.() ?? _privateMessage
|
|
391
396
|
this._privateChannel = _privateChannel
|
|
392
397
|
this._getRelaysByPubkey = _getRelaysByPubkey
|
|
@@ -1332,13 +1337,18 @@ export class PrivateMessenger {
|
|
|
1332
1337
|
privateChannelReaderSigner: channel.readerSigner,
|
|
1333
1338
|
privateChannelReaderPubkey: channel.readerPubkey,
|
|
1334
1339
|
mode: channel.mode,
|
|
1335
|
-
onAsk: message => this.receive(pubkey, () => this.handleAsk(pubkey, message), message),
|
|
1336
|
-
onReply: message => this.receive(pubkey, () => this.handleReply(pubkey, message), message),
|
|
1337
|
-
onTell: message => this.receive(pubkey, () => this.handleTell(pubkey, message), message),
|
|
1338
|
-
onYell: message => this.receive(pubkey, () => this.handleYell(pubkey, message), message),
|
|
1339
|
-
onNym: message => this.receive(pubkey, () => this.handleNym(pubkey, message), message),
|
|
1340
|
-
onMessage: message =>
|
|
1341
|
-
|
|
1340
|
+
onAsk: message => this.receive(pubkey, () => this.handleAsk(pubkey, message), message, { inboundType: 'ask' }),
|
|
1341
|
+
onReply: message => this.receive(pubkey, () => this.handleReply(pubkey, message), message, { inboundType: 'reply' }),
|
|
1342
|
+
onTell: message => this.receive(pubkey, () => this.handleTell(pubkey, message), message, { inboundType: 'tell' }),
|
|
1343
|
+
onYell: message => this.receive(pubkey, () => this.handleYell(pubkey, message), message, { inboundType: 'yell' }),
|
|
1344
|
+
onNym: message => this.receive(pubkey, () => this.handleNym(pubkey, message), message, { inboundType: 'nym' }),
|
|
1345
|
+
onMessage: message => {
|
|
1346
|
+
const type = eventType(message.event)
|
|
1347
|
+
if (type !== 'message' && message.provenance !== 'hearsay' &&
|
|
1348
|
+
(!message.senderPubkey || message.senderPubkey === message.event.pubkey)) return Promise.resolve()
|
|
1349
|
+
return this.receive(pubkey, () => this.handleMessage(pubkey, message), message, { inboundType: 'message' })
|
|
1350
|
+
},
|
|
1351
|
+
onSeed: seed => this.receive(pubkey, () => this.enqueueSeed(pubkey, seed), seed, { inboundType: 'seed' }),
|
|
1342
1352
|
onContentKeyUsage: usage => this.handleContentKeyUsage(pubkey, usage),
|
|
1343
1353
|
onDecryptionFailure: failure => this.deferDecryption(failure),
|
|
1344
1354
|
_onProcessing: pubkey => this.decryptionScheduler.activity(pubkey),
|
|
@@ -2102,7 +2112,44 @@ export class PrivateMessenger {
|
|
|
2102
2112
|
return jobs
|
|
2103
2113
|
}
|
|
2104
2114
|
|
|
2105
|
-
|
|
2115
|
+
#admitInboundPeer (pubkey, inboundType, message) {
|
|
2116
|
+
if (!this.admitInboundPeer) return true
|
|
2117
|
+
const type = inboundType || 'message'
|
|
2118
|
+
const context = {
|
|
2119
|
+
type,
|
|
2120
|
+
channelPubkey: pubkey,
|
|
2121
|
+
senderPubkey: inboundSenderPubkey(message),
|
|
2122
|
+
code: messageCode(message) || message?.recordType || '',
|
|
2123
|
+
bytes: inboundMessageBytes(message),
|
|
2124
|
+
now: this._now()
|
|
2125
|
+
}
|
|
2126
|
+
let admitted
|
|
2127
|
+
try {
|
|
2128
|
+
admitted = this.admitInboundPeer(
|
|
2129
|
+
{ ...message, type, channelPubkey: pubkey },
|
|
2130
|
+
context
|
|
2131
|
+
)
|
|
2132
|
+
if (admitted != null && typeof admitted.then === 'function') {
|
|
2133
|
+
throw new Error('INBOUND_PEER_ADMISSION_MUST_BE_SYNCHRONOUS')
|
|
2134
|
+
}
|
|
2135
|
+
} catch (cause) {
|
|
2136
|
+
this.onError?.(inboundPeerAdmissionError(cause))
|
|
2137
|
+
return true
|
|
2138
|
+
}
|
|
2139
|
+
if (admitted === false) {
|
|
2140
|
+
this.debug('inbound-peer-dropped', {
|
|
2141
|
+
type,
|
|
2142
|
+
code: context.code
|
|
2143
|
+
})
|
|
2144
|
+
return false
|
|
2145
|
+
}
|
|
2146
|
+
if (admitted !== true) {
|
|
2147
|
+
this.onError?.(inboundPeerAdmissionError(new ValidationError('INVALID_INBOUND_PEER_ADMISSION')))
|
|
2148
|
+
}
|
|
2149
|
+
return true
|
|
2150
|
+
}
|
|
2151
|
+
|
|
2152
|
+
receive (pubkey, operation, message = {}, { trackActivity = true, inboundType = 'message' } = {}) {
|
|
2106
2153
|
if (this.closePromise) {
|
|
2107
2154
|
if (this.storageActive) this.recordInterruption([pubkey], messageTime(message))
|
|
2108
2155
|
return Promise.reject(new Error('PRIVATE_MESSENGER_CLOSED'))
|
|
@@ -2113,6 +2160,7 @@ export class PrivateMessenger {
|
|
|
2113
2160
|
this.recordInterruption([pubkey], messageTime(message))
|
|
2114
2161
|
throw this.pausedError()
|
|
2115
2162
|
}
|
|
2163
|
+
if (!this.#admitInboundPeer(pubkey, inboundType, message)) return
|
|
2116
2164
|
return operation()
|
|
2117
2165
|
}).finally(releaseActivity)
|
|
2118
2166
|
}
|
|
@@ -3373,9 +3421,9 @@ export class PrivateMessenger {
|
|
|
3373
3421
|
modeByPubkey: { [channelPubkey]: channel.mode },
|
|
3374
3422
|
receivedChunkTtlMs: this.receivedChunkTtlMsFor(channel),
|
|
3375
3423
|
receivedChunkIndexedDB: this._indexedDB,
|
|
3376
|
-
onEvent: (event, outer, meta) => this.receive(channelPubkey, () => this.enqueueRumor(eventType(event), channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }),
|
|
3377
|
-
onNymEvent: (event, outer, meta) => this.receive(channelPubkey, () => this.enqueueRumor('nym', channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }),
|
|
3378
|
-
onSeedEvent: seed => this.receive(channelPubkey, () => this.enqueueSeed(channelPubkey, seed), seed),
|
|
3424
|
+
onEvent: (event, outer, meta) => this.receive(channelPubkey, () => this.enqueueRumor(eventType(event), channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }, { inboundType: eventType(event) }),
|
|
3425
|
+
onNymEvent: (event, outer, meta) => this.receive(channelPubkey, () => this.enqueueRumor('nym', channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }, { inboundType: 'nym' }),
|
|
3426
|
+
onSeedEvent: seed => this.receive(channelPubkey, () => this.enqueueSeed(channelPubkey, seed), seed, { inboundType: 'seed' }),
|
|
3379
3427
|
onContentKeyUsage: usage => this.handleContentKeyUsage(channelPubkey, usage),
|
|
3380
3428
|
onDecryptionFailure: failure => this.deferDecryption(failure),
|
|
3381
3429
|
_onProcessing: pubkey => this.decryptionScheduler.activity(pubkey),
|
|
@@ -3731,13 +3779,13 @@ export class PrivateMessenger {
|
|
|
3731
3779
|
},
|
|
3732
3780
|
onEvent: (event, outer, meta) => {
|
|
3733
3781
|
current()
|
|
3734
|
-
return this.receive(channelPubkey, () => this.enqueueRumor(eventType(event), channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }, { trackActivity: false })
|
|
3782
|
+
return this.receive(channelPubkey, () => this.enqueueRumor(eventType(event), channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }, { trackActivity: false, inboundType: eventType(event) })
|
|
3735
3783
|
},
|
|
3736
3784
|
onNymEvent: (event, outer, meta) => {
|
|
3737
3785
|
current()
|
|
3738
|
-
return this.receive(channelPubkey, () => this.enqueueRumor('nym', channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }, { trackActivity: false })
|
|
3786
|
+
return this.receive(channelPubkey, () => this.enqueueRumor('nym', channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }, { trackActivity: false, inboundType: 'nym' })
|
|
3739
3787
|
},
|
|
3740
|
-
onSeedEvent: seed => { current(); return this.receive(channelPubkey, () => this.enqueueSeed(channelPubkey, seed), seed, { trackActivity: false }) },
|
|
3788
|
+
onSeedEvent: seed => { current(); return this.receive(channelPubkey, () => this.enqueueSeed(channelPubkey, seed), seed, { trackActivity: false, inboundType: 'seed' }) },
|
|
3741
3789
|
onRecovered: async record => {
|
|
3742
3790
|
current()
|
|
3743
3791
|
await callbacks.onRecovered?.(record)
|
|
@@ -4058,6 +4106,31 @@ function messageTime (message) {
|
|
|
4058
4106
|
return message.outer?.created_at || message.event?.created_at || nowSeconds()
|
|
4059
4107
|
}
|
|
4060
4108
|
|
|
4109
|
+
function inboundSenderPubkey (message) {
|
|
4110
|
+
return message?.senderPubkey ||
|
|
4111
|
+
message?.event?.pubkey ||
|
|
4112
|
+
message?.meta?.senderPubkey ||
|
|
4113
|
+
message?.router?.tags?.find(tag => tag[0] === 'f')?.[1] ||
|
|
4114
|
+
''
|
|
4115
|
+
}
|
|
4116
|
+
|
|
4117
|
+
function inboundMessageBytes (message) {
|
|
4118
|
+
const value = message?.event
|
|
4119
|
+
? message.event
|
|
4120
|
+
: message?.recordType ? message : message?.router || message
|
|
4121
|
+
try {
|
|
4122
|
+
return encoder.encode(JSON.stringify(value)).byteLength
|
|
4123
|
+
} catch {
|
|
4124
|
+
return 0
|
|
4125
|
+
}
|
|
4126
|
+
}
|
|
4127
|
+
|
|
4128
|
+
function inboundPeerAdmissionError (cause) {
|
|
4129
|
+
return Object.assign(new Error(cause?.message || 'INVALID_INBOUND_PEER_ADMISSION', { cause }), {
|
|
4130
|
+
code: 'INVALID_INBOUND_PEER_ADMISSION'
|
|
4131
|
+
})
|
|
4132
|
+
}
|
|
4133
|
+
|
|
4061
4134
|
function oldestCreatedAt (events) {
|
|
4062
4135
|
let oldest = null
|
|
4063
4136
|
for (const event of events || []) {
|