libp2r2p 0.12.14 → 0.12.15
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 +23 -8
- package/package.json +1 -1
- package/private-messenger/index.js +131 -21
package/README.md
CHANGED
|
@@ -370,14 +370,29 @@ where `key` is a lowercase 64-character SHA-256 digest, `keep` is `latest`
|
|
|
370
370
|
Sequence units are resolver-owned and are compared only within records sharing
|
|
371
371
|
the final scoped coalescing key.
|
|
372
372
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
373
|
+
Every inbox type first attempts exact deduplication by channel, type, event ID
|
|
374
|
+
and provenance, then a valid resolver key, then the automatic `D` fallback for
|
|
375
|
+
ask/reply/tell/yell. When none supplies an identity, ask, reply, tell, yell,
|
|
376
|
+
message and nym fall back to Nostr identity within the same channel and type:
|
|
377
|
+
the first usable `d` tag selects an address, replaceable/addressable events
|
|
378
|
+
without a usable `d` use `""`, and regular or ephemeral events use `event.id`.
|
|
379
|
+
Ephemeral events never use an address. An explicit `d` is not merged with a
|
|
380
|
+
regular event that has no address.
|
|
381
|
+
|
|
382
|
+
Resolver and `D` keys retain channel, sender, type, code and provenance scope.
|
|
383
|
+
Automatic keys omit sender, provenance and code. For copies with the same event
|
|
384
|
+
ID, provenance orders `signed > direct > hearsay`. Different versions of an
|
|
385
|
+
address follow NIP-01: higher inner `created_at`, then lower event ID on a tie;
|
|
386
|
+
`signed` and `direct` compare as legitimate versions, while hearsay never
|
|
387
|
+
displaces either. Address ordering ignores outer time and the `ms` tag.
|
|
388
|
+
|
|
389
|
+
Resolver/idempotency `latest` replaces the pending item in place. Its order is:
|
|
390
|
+
explicit sequence; `eventCreatedAtMs`, read from the inner `created_at` plus the
|
|
391
|
+
`ms` tag; outer `created_at`; event ID. `oldest` preserves the pending item.
|
|
392
|
+
While an item is reserved, all policies allow at most one successor. Ordering
|
|
393
|
+
metadata persists internally, is removed before app delivery, and older pending
|
|
394
|
+
records remain valid until drained. Older recovered events do not replace newer
|
|
395
|
+
requests.
|
|
381
396
|
|
|
382
397
|
New high-level ask, reply, tell and yell rumors include exactly one
|
|
383
398
|
`["ms", "0".."999"]` tag and use the same instant to derive inner `created_at`.
|
package/package.json
CHANGED
|
@@ -70,6 +70,7 @@ import { getRelaysByPubkey, pickRelaysForPubkeys, subscribeRelayListUpdates, rel
|
|
|
70
70
|
import * as privateChannel from '../private-channel/index.js'
|
|
71
71
|
import { DEFAULT_RECEIVED_CHUNK_TTL_MS } from '../private-channel/services/received-chunks.js'
|
|
72
72
|
import { createQueue } from '../idb-queue/index.js'
|
|
73
|
+
import { classifyEvent, isEphemeralEvent } from '../event/index.js'
|
|
73
74
|
import { createChannelStateStore } from './services/channel-state.js'
|
|
74
75
|
import { normalizeIdempotencyKey, readIdempotencyKey } from '../private-message/helpers/idempotency.js'
|
|
75
76
|
import { eventCreatedAtMs, readMilliseconds } from '../private-message/helpers/timestamp.js'
|
|
@@ -131,6 +132,11 @@ const COALESCING_INDEX = 'byCoalescingKey'
|
|
|
131
132
|
const COALESCING_DIGEST = /^[0-9a-f]{64}$/
|
|
132
133
|
const COALESCING_KEEP = new Set(['oldest', 'latest'])
|
|
133
134
|
const DEDUPE_TYPES = new Set(['ask', 'reply', 'tell', 'yell'])
|
|
135
|
+
const PROVENANCE_PRIORITY = {
|
|
136
|
+
signed: 2,
|
|
137
|
+
direct: 1,
|
|
138
|
+
hearsay: 0
|
|
139
|
+
}
|
|
134
140
|
const TIMESTAMPED_PRIVATE_MESSAGE_KINDS = new Set([
|
|
135
141
|
privateMessage.ASK_KIND,
|
|
136
142
|
privateMessage.REPLY_KIND,
|
|
@@ -2309,6 +2315,7 @@ export class PrivateMessenger {
|
|
|
2309
2315
|
let digest = null
|
|
2310
2316
|
let keep = 'latest'
|
|
2311
2317
|
let sequence = null
|
|
2318
|
+
let policy = 'semantic'
|
|
2312
2319
|
if (resolved != null) {
|
|
2313
2320
|
if (isPlainObject(resolved) && typeof resolved.key === 'string' &&
|
|
2314
2321
|
COALESCING_DIGEST.test(resolved.key) &&
|
|
@@ -2322,27 +2329,37 @@ export class PrivateMessenger {
|
|
|
2322
2329
|
this.onError?.(coalescingError(new ValidationError('INVALID_INBOUND_COALESCING_RESULT')))
|
|
2323
2330
|
}
|
|
2324
2331
|
}
|
|
2325
|
-
if (!digest && tagged.key)
|
|
2326
|
-
|
|
2332
|
+
if (!digest && tagged.key) {
|
|
2333
|
+
digest = idempotencyDigest(tagged.key)
|
|
2334
|
+
policy = 'idempotency'
|
|
2335
|
+
}
|
|
2327
2336
|
|
|
2328
2337
|
const senderPubkey = info.senderPubkey || message.event?.pubkey || ''
|
|
2329
2338
|
const code = messageCode(message)
|
|
2330
|
-
if (!channelPubkey || !senderPubkey || typeof code !== 'string' || !info.provenance) {
|
|
2339
|
+
if (digest && (!channelPubkey || !senderPubkey || typeof code !== 'string' || !info.provenance)) {
|
|
2331
2340
|
this.onError?.(coalescingError(new ValidationError('INVALID_INBOUND_COALESCING_SCOPE')))
|
|
2332
|
-
|
|
2341
|
+
digest = null
|
|
2333
2342
|
}
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2343
|
+
if (digest) {
|
|
2344
|
+
return {
|
|
2345
|
+
coalescingKey: JSON.stringify([
|
|
2346
|
+
channelPubkey,
|
|
2347
|
+
senderPubkey,
|
|
2348
|
+
type,
|
|
2349
|
+
code,
|
|
2350
|
+
info.provenance,
|
|
2351
|
+
digest
|
|
2352
|
+
]),
|
|
2353
|
+
coalescingKeep: keep,
|
|
2354
|
+
coalescingOrder: coalescingOrder(message, {
|
|
2355
|
+
policy,
|
|
2356
|
+
sequence,
|
|
2357
|
+
eventCreatedAtMs: messageCreatedAtMs
|
|
2358
|
+
})
|
|
2359
|
+
}
|
|
2345
2360
|
}
|
|
2361
|
+
|
|
2362
|
+
return automaticCoalescingMetadata(type, channelPubkey, message, info)
|
|
2346
2363
|
}
|
|
2347
2364
|
|
|
2348
2365
|
async enqueueRumor (type, channelPubkey, message) {
|
|
@@ -2350,7 +2367,7 @@ export class PrivateMessenger {
|
|
|
2350
2367
|
if (channel?.mode === 'watchtower' && type !== 'ask') return
|
|
2351
2368
|
const info = deliveryInfo(message.event, message.senderPubkey ?? message.meta?.senderPubkey ?? message.meta?.router?.tags?.find(tag => tag[0] === 'f')?.[1])
|
|
2352
2369
|
const eventId = message.event?.id || ''
|
|
2353
|
-
//
|
|
2370
|
+
// Exact copies are dropped; stronger provenance participates in coalescing below.
|
|
2354
2371
|
const dedupeKey = eventId ? [channelPubkey, type, eventId, info.provenance] : null
|
|
2355
2372
|
if (dedupeKey && await this.queue.someBy('byChannelTypeEventId', dedupeKey)) {
|
|
2356
2373
|
this.markSeen(channelPubkey, message.outer?.created_at || message.event?.created_at || nowSeconds())
|
|
@@ -2375,8 +2392,9 @@ export class PrivateMessenger {
|
|
|
2375
2392
|
meta: message.meta || null
|
|
2376
2393
|
}
|
|
2377
2394
|
Object.assign(record, this.#coalescingMetadata(type, channelPubkey, message, info) || {})
|
|
2395
|
+
let coalesced = null
|
|
2378
2396
|
try {
|
|
2379
|
-
|
|
2397
|
+
coalesced = record.coalescingKey
|
|
2380
2398
|
? await this.queue.coalesceBy(COALESCING_INDEX, record, {
|
|
2381
2399
|
compare: compareCoalescingItems,
|
|
2382
2400
|
keep: record.coalescingKeep
|
|
@@ -2387,6 +2405,7 @@ export class PrivateMessenger {
|
|
|
2387
2405
|
this.markSeen(channelPubkey, message.outer?.created_at || message.event?.created_at || nowSeconds())
|
|
2388
2406
|
this.debug('message-coalescing-ignored', {
|
|
2389
2407
|
code: messageCode(message) || '',
|
|
2408
|
+
policy: record.coalescingOrder?.policy || 'legacy',
|
|
2390
2409
|
keep: record.coalescingKeep,
|
|
2391
2410
|
payloadBytes: encoder.encode(JSON.stringify(record)).byteLength
|
|
2392
2411
|
})
|
|
@@ -2395,6 +2414,7 @@ export class PrivateMessenger {
|
|
|
2395
2414
|
if (coalesced) {
|
|
2396
2415
|
this.debug('message-coalesced', {
|
|
2397
2416
|
code: messageCode(message) || '',
|
|
2417
|
+
policy: record.coalescingOrder?.policy || 'legacy',
|
|
2398
2418
|
keep: record.coalescingKeep,
|
|
2399
2419
|
status: coalesced.status,
|
|
2400
2420
|
removedUnreserved: coalesced.removed,
|
|
@@ -2434,7 +2454,9 @@ export class PrivateMessenger {
|
|
|
2434
2454
|
}
|
|
2435
2455
|
this.markSeen(channelPubkey, message.outer?.created_at || message.event?.created_at || nowSeconds())
|
|
2436
2456
|
this.wakeDeliveries()
|
|
2437
|
-
if (!record.coalescingKey
|
|
2457
|
+
if (!record.coalescingKey || coalesced?.status === 'inserted') {
|
|
2458
|
+
this.debug('enqueue', debugMessageInfo(type, channelPubkey, message))
|
|
2459
|
+
}
|
|
2438
2460
|
this.onMessageQueued?.()
|
|
2439
2461
|
}
|
|
2440
2462
|
|
|
@@ -4013,9 +4035,65 @@ function messageCode (message) {
|
|
|
4013
4035
|
: null
|
|
4014
4036
|
}
|
|
4015
4037
|
|
|
4016
|
-
function
|
|
4038
|
+
function automaticCoalescingMetadata (type, channelPubkey, message, info) {
|
|
4039
|
+
const identity = automaticEventIdentity(message.event)
|
|
4040
|
+
if (!identity || !channelPubkey || !type || !info.provenance) return null
|
|
4041
|
+
const eventIdPriority = PROVENANCE_PRIORITY[info.provenance] ?? 0
|
|
4042
|
+
return {
|
|
4043
|
+
coalescingKey: JSON.stringify([
|
|
4044
|
+
channelPubkey,
|
|
4045
|
+
type,
|
|
4046
|
+
'nostr-identity',
|
|
4047
|
+
identity.kind,
|
|
4048
|
+
identity.digest
|
|
4049
|
+
]),
|
|
4050
|
+
coalescingKeep: 'latest',
|
|
4051
|
+
coalescingOrder: identity.kind === 'address'
|
|
4052
|
+
? {
|
|
4053
|
+
policy: 'event-address',
|
|
4054
|
+
eventIdPriority,
|
|
4055
|
+
inner: Number.isSafeInteger(message.event?.created_at) ? message.event.created_at : 0,
|
|
4056
|
+
eventId: message.event.id
|
|
4057
|
+
}
|
|
4058
|
+
: {
|
|
4059
|
+
policy: 'event-id',
|
|
4060
|
+
eventIdPriority
|
|
4061
|
+
}
|
|
4062
|
+
}
|
|
4063
|
+
}
|
|
4064
|
+
|
|
4065
|
+
function automaticEventIdentity (event) {
|
|
4066
|
+
if (!event || typeof event.id !== 'string' || !event.id) return null
|
|
4067
|
+
if (!isEphemeralEvent(event)) {
|
|
4068
|
+
const firstD = event.tags?.find(tag => Array.isArray(tag) && tag[0] === 'd')
|
|
4069
|
+
let coordinate = null
|
|
4070
|
+
if (typeof firstD?.[1] === 'string') {
|
|
4071
|
+
coordinate = firstD[1]
|
|
4072
|
+
} else {
|
|
4073
|
+
const classes = classifyEvent(event)
|
|
4074
|
+
if (classes.includes('replaceable') || classes.includes('addressable')) coordinate = ''
|
|
4075
|
+
}
|
|
4076
|
+
if (coordinate !== null && Number.isSafeInteger(event.kind) && event.kind >= 0 && typeof event.pubkey === 'string') {
|
|
4077
|
+
return {
|
|
4078
|
+
kind: 'address',
|
|
4079
|
+
digest: coalescingDigest(['nostr-address:v1', event.kind, event.pubkey, coordinate])
|
|
4080
|
+
}
|
|
4081
|
+
}
|
|
4082
|
+
}
|
|
4083
|
+
return {
|
|
4084
|
+
kind: 'event-id',
|
|
4085
|
+
digest: coalescingDigest(['nostr-event-id:v1', event.id])
|
|
4086
|
+
}
|
|
4087
|
+
}
|
|
4088
|
+
|
|
4089
|
+
function coalescingOrder (message, {
|
|
4090
|
+
policy = 'semantic',
|
|
4091
|
+
sequence = null,
|
|
4092
|
+
eventCreatedAtMs = 0
|
|
4093
|
+
} = {}) {
|
|
4017
4094
|
const order = {
|
|
4018
|
-
|
|
4095
|
+
policy,
|
|
4096
|
+
eventTimeMs: eventCreatedAtMs,
|
|
4019
4097
|
outer: Number.isSafeInteger(message?.outer?.created_at) ? message.outer.created_at : 0,
|
|
4020
4098
|
inner: Number.isSafeInteger(message?.event?.created_at) ? message.event.created_at : 0,
|
|
4021
4099
|
eventId: message?.event?.id || ''
|
|
@@ -4025,6 +4103,34 @@ function coalescingOrder (message, sequence = null, messageCreatedAtMs = 0) {
|
|
|
4025
4103
|
}
|
|
4026
4104
|
|
|
4027
4105
|
function compareCoalescingOrder (left, right) {
|
|
4106
|
+
if (left?.policy === 'event-id' || right?.policy === 'event-id') {
|
|
4107
|
+
return compareEventIdCoalescingOrder(left, right)
|
|
4108
|
+
}
|
|
4109
|
+
if (left?.policy === 'event-address' || right?.policy === 'event-address') {
|
|
4110
|
+
return compareAddressCoalescingOrder(left, right)
|
|
4111
|
+
}
|
|
4112
|
+
return compareLegacyCoalescingOrder(left, right)
|
|
4113
|
+
}
|
|
4114
|
+
|
|
4115
|
+
function compareEventIdCoalescingOrder (left, right) {
|
|
4116
|
+
return (left?.eventIdPriority || 0) - (right?.eventIdPriority || 0)
|
|
4117
|
+
}
|
|
4118
|
+
|
|
4119
|
+
function compareAddressCoalescingOrder (left, right) {
|
|
4120
|
+
const leftId = left?.eventId || ''
|
|
4121
|
+
const rightId = right?.eventId || ''
|
|
4122
|
+
if (leftId === rightId) {
|
|
4123
|
+
return (left?.eventIdPriority || 0) - (right?.eventIdPriority || 0)
|
|
4124
|
+
}
|
|
4125
|
+
const trust = ((left?.eventIdPriority || 0) > 0 ? 1 : 0) - ((right?.eventIdPriority || 0) > 0 ? 1 : 0)
|
|
4126
|
+
if (trust) return trust
|
|
4127
|
+
const leftTime = Number.isSafeInteger(left?.inner) ? left.inner : 0
|
|
4128
|
+
const rightTime = Number.isSafeInteger(right?.inner) ? right.inner : 0
|
|
4129
|
+
if (leftTime !== rightTime) return leftTime < rightTime ? -1 : 1
|
|
4130
|
+
return leftId < rightId ? 1 : -1
|
|
4131
|
+
}
|
|
4132
|
+
|
|
4133
|
+
function compareLegacyCoalescingOrder (left, right) {
|
|
4028
4134
|
if (Number.isSafeInteger(left?.sequence) && Number.isSafeInteger(right?.sequence) &&
|
|
4029
4135
|
left.sequence !== right.sequence) {
|
|
4030
4136
|
return left.sequence < right.sequence ? -1 : 1
|
|
@@ -4048,7 +4154,11 @@ function compareCoalescingItems (left, right) {
|
|
|
4048
4154
|
}
|
|
4049
4155
|
|
|
4050
4156
|
function idempotencyDigest (idempotencyKey) {
|
|
4051
|
-
return
|
|
4157
|
+
return coalescingDigest(['idempotency', idempotencyKey])
|
|
4158
|
+
}
|
|
4159
|
+
|
|
4160
|
+
function coalescingDigest (value) {
|
|
4161
|
+
return bytesToHex(sha256(encoder.encode(JSON.stringify(value))))
|
|
4052
4162
|
}
|
|
4053
4163
|
|
|
4054
4164
|
function coalescingError (cause) {
|