libp2r2p 0.12.12 → 0.12.13
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 -13
- package/package.json +1 -1
- package/private-message/helpers/timestamp.js +69 -0
- package/private-message/index.js +31 -4
- package/private-messenger/index.js +36 -5
package/README.md
CHANGED
|
@@ -362,21 +362,29 @@ await messenger.tell({
|
|
|
362
362
|
### Inbound message coalescing
|
|
363
363
|
|
|
364
364
|
`PrivateMessenger` accepts an optional synchronous
|
|
365
|
-
`resolveInboundCoalescing(message, { idempotencyKey })`
|
|
366
|
-
The enriched `message` includes `type` and
|
|
367
|
-
`null` or `{ key, keep?, sequence? }`,
|
|
368
|
-
SHA-256 digest, `keep` is `latest`
|
|
369
|
-
optional non-negative safe integer.
|
|
370
|
-
compared only within records sharing
|
|
365
|
+
`resolveInboundCoalescing(message, { idempotencyKey, eventCreatedAtMs })`
|
|
366
|
+
constructor callback. The enriched `message` includes `type` and
|
|
367
|
+
`channelPubkey`. The callback returns `null` or `{ key, keep?, sequence? }`,
|
|
368
|
+
where `key` is a lowercase 64-character SHA-256 digest, `keep` is `latest`
|
|
369
|
+
(default) or `oldest`, and `sequence` is an optional non-negative safe integer.
|
|
370
|
+
Sequence units are resolver-owned and are compared only within records sharing
|
|
371
|
+
the final scoped coalescing key.
|
|
371
372
|
|
|
372
373
|
The resolver runs for ask, reply, tell, yell, message and nym deliveries after
|
|
373
374
|
exact event-ID deduplication. The final coalescing key is scoped by channel,
|
|
374
375
|
sender, type, message code and provenance. `latest` replaces the pending item in
|
|
375
|
-
place
|
|
376
|
-
`created_at
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
376
|
+
place. Its order is: explicit sequence; `eventCreatedAtMs`, read from the inner
|
|
377
|
+
`created_at` plus the `ms` tag; outer `created_at`; event ID. `oldest` preserves
|
|
378
|
+
the pending item. While an item is reserved, both policies allow at most one
|
|
379
|
+
successor. Timestamp and sequence ordering persist in internal queue metadata.
|
|
380
|
+
Older recovered events do not replace newer requests.
|
|
381
|
+
|
|
382
|
+
New high-level ask, reply, tell and yell rumors include exactly one
|
|
383
|
+
`["ms", "0".."999"]` tag and use the same instant to derive inner `created_at`.
|
|
384
|
+
Missing `ms` means zero for legacy messages; malformed or duplicate tags also
|
|
385
|
+
order as zero and are reported only through sanitized debug diagnostics.
|
|
386
|
+
`message`, `nym`, broadcast rumors and signed-event APIs do not interpret `ms`
|
|
387
|
+
automatically.
|
|
380
388
|
|
|
381
389
|
For ask, reply, tell and yell, exactly one `["D", key]` tag may serve as an
|
|
382
390
|
automatic fallback when the resolver returns `null`. Its key is non-empty and
|
|
@@ -1377,8 +1385,10 @@ signatures, and routed only for the pending peers. The legacy
|
|
|
1377
1385
|
`preparedPublication` public argument is rejected with
|
|
1378
1386
|
`LEGACY_PREPARED_PUBLICATION`; existing encrypted outbox snapshots containing
|
|
1379
1387
|
`prepared.publication` are migrated internally to
|
|
1380
|
-
`prepared.publications` before processing. `yell`
|
|
1381
|
-
|
|
1388
|
+
`prepared.publications` before processing. New `yell` replays pass the original
|
|
1389
|
+
`createdAtMs` so the inner rumor identity matches. Legacy prepared inners
|
|
1390
|
+
without `ms` are detected and replayed without adding the tag; a prepared inner
|
|
1391
|
+
that requires an unavailable `createdAtMs` fails with `CREATED_AT_MS_REQUIRED`.
|
|
1382
1392
|
`broadcastRumor` and `broadcastEvent` already carry their original inner event.
|
|
1383
1393
|
Nym broadcasts intentionally remain a single carrier union.
|
|
1384
1394
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { ValidationError } from '../../error/index.js'
|
|
2
|
+
|
|
3
|
+
export const MILLISECONDS_TAG = 'ms'
|
|
4
|
+
|
|
5
|
+
const CANONICAL_MILLISECONDS = /^(0|[1-9][0-9]{0,2})$/
|
|
6
|
+
|
|
7
|
+
function normalizeCreatedAt (createdAt) {
|
|
8
|
+
if (!Number.isSafeInteger(createdAt) || createdAt < 0) throw new ValidationError('INVALID_CREATED_AT')
|
|
9
|
+
return createdAt
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function timestampFromMilliseconds (createdAtMs) {
|
|
13
|
+
if (!Number.isSafeInteger(createdAtMs) || createdAtMs < 0) {
|
|
14
|
+
throw new ValidationError('INVALID_CREATED_AT_MS')
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
createdAt: Math.floor(createdAtMs / 1000),
|
|
18
|
+
milliseconds: createdAtMs % 1000
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function messageTimestamp ({ createdAt, createdAtMs, nowMs = Date.now } = {}) {
|
|
23
|
+
if (createdAtMs != null) {
|
|
24
|
+
const timestamp = timestampFromMilliseconds(createdAtMs)
|
|
25
|
+
if (createdAt != null && normalizeCreatedAt(createdAt) !== timestamp.createdAt) {
|
|
26
|
+
throw new ValidationError('INVALID_CREATED_AT_MS')
|
|
27
|
+
}
|
|
28
|
+
return timestamp
|
|
29
|
+
}
|
|
30
|
+
if (createdAt != null) return { createdAt: normalizeCreatedAt(createdAt), milliseconds: 0 }
|
|
31
|
+
const current = nowMs()
|
|
32
|
+
if (!Number.isSafeInteger(current) || current < 0) throw new ValidationError('INVALID_CREATED_AT_MS')
|
|
33
|
+
return {
|
|
34
|
+
createdAt: Math.floor(current / 1000),
|
|
35
|
+
milliseconds: current % 1000
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function setMillisecondsTag (tags, milliseconds) {
|
|
40
|
+
if (!Number.isSafeInteger(milliseconds) || milliseconds < 0 || milliseconds > 999) {
|
|
41
|
+
throw new ValidationError('INVALID_MILLISECONDS')
|
|
42
|
+
}
|
|
43
|
+
const out = (Array.isArray(tags) ? tags : [])
|
|
44
|
+
.filter(tag => !Array.isArray(tag) || tag[0] !== MILLISECONDS_TAG)
|
|
45
|
+
.map(tag => Array.isArray(tag) ? [...tag] : tag)
|
|
46
|
+
out.push([MILLISECONDS_TAG, String(milliseconds)])
|
|
47
|
+
return out
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function readMilliseconds (event) {
|
|
51
|
+
const tags = (event?.tags || []).filter(tag => Array.isArray(tag) && tag[0] === MILLISECONDS_TAG)
|
|
52
|
+
if (!tags.length) return { milliseconds: 0, reason: 'missing' }
|
|
53
|
+
if (tags.length !== 1 || tags[0].length !== 2 || !CANONICAL_MILLISECONDS.test(tags[0][1])) {
|
|
54
|
+
return { milliseconds: 0, reason: tags.length === 1 ? 'invalid' : 'duplicate' }
|
|
55
|
+
}
|
|
56
|
+
const milliseconds = Number(tags[0][1])
|
|
57
|
+
if (!Number.isInteger(milliseconds) || milliseconds < 0 || milliseconds > 999) {
|
|
58
|
+
return { milliseconds: 0, reason: 'invalid' }
|
|
59
|
+
}
|
|
60
|
+
return { milliseconds, reason: '' }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function eventCreatedAtMs (event, { readMillisecondsTag = true } = {}) {
|
|
64
|
+
if (!Number.isSafeInteger(event?.created_at) || event.created_at < 0) return 0
|
|
65
|
+
const { milliseconds } = readMillisecondsTag
|
|
66
|
+
? readMilliseconds(event)
|
|
67
|
+
: { milliseconds: 0 }
|
|
68
|
+
return event.created_at * 1000 + milliseconds
|
|
69
|
+
}
|
package/private-message/index.js
CHANGED
|
@@ -6,6 +6,7 @@ import * as privateChannel from '../private-channel/index.js'
|
|
|
6
6
|
import { normalizeRumor, deliveryInfo } from '../private-channel/helpers/rumor.js'
|
|
7
7
|
import { normalizePreparedPublications } from '../private-channel/helpers/prepared.js'
|
|
8
8
|
import { appendIdempotencyTag } from './helpers/idempotency.js'
|
|
9
|
+
import { messageTimestamp, setMillisecondsTag } from './helpers/timestamp.js'
|
|
9
10
|
|
|
10
11
|
export const ASK_KIND = 7329
|
|
11
12
|
export const REPLY_KIND = 7330
|
|
@@ -153,18 +154,38 @@ function cloneTags (tags) {
|
|
|
153
154
|
return tags.map(tag => Array.isArray(tag) ? [...tag] : tag)
|
|
154
155
|
}
|
|
155
156
|
|
|
156
|
-
async function makeOutgoingRumor ({ senderSigner, rumor }) {
|
|
157
|
+
async function makeOutgoingRumor ({ senderSigner, rumor, timestamped = false, createdAtMs }) {
|
|
157
158
|
if (!senderSigner?.getPublicKey) throw new ValidationError('SENDER_SIGNER_REQUIRED')
|
|
158
159
|
const senderPubkey = await senderSigner.getPublicKey()
|
|
159
160
|
if (rumor.pubkey !== undefined && rumor.pubkey !== senderPubkey && rumor.created_at === undefined) {
|
|
160
161
|
throw new ValidationError('FORWARDED_RUMOR_TIMESTAMP_REQUIRED')
|
|
161
162
|
}
|
|
162
|
-
const
|
|
163
|
+
const timestamp = messageTimestamp({ createdAt: rumor.created_at, createdAtMs })
|
|
164
|
+
const tags = timestamped
|
|
165
|
+
? setMillisecondsTag(rumor.tags, timestamp.milliseconds)
|
|
166
|
+
: cloneTags(rumor.tags)
|
|
167
|
+
const event = normalizeRumor({ ...rumor, tags, created_at: timestamp.createdAt }, senderPubkey)
|
|
163
168
|
const { id: _, pubkey, ...template } = event
|
|
164
169
|
const wireEvent = pubkey === senderPubkey ? template : { ...template, pubkey }
|
|
165
170
|
return { event, wireEvent }
|
|
166
171
|
}
|
|
167
172
|
|
|
173
|
+
function preparedInnerEventsMatch (publications, event) {
|
|
174
|
+
return publications.length > 0 && publications.every(publication => (
|
|
175
|
+
publication?.innerEventId && publication.innerEventId === event?.id
|
|
176
|
+
))
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function makeYellRumor ({ senderSigner, rumor, createdAtMs, prepared }) {
|
|
180
|
+
const timestamped = await makeOutgoingRumor({ senderSigner, rumor, timestamped: true, createdAtMs })
|
|
181
|
+
if (!prepared.length || preparedInnerEventsMatch(prepared, timestamped.event)) return timestamped
|
|
182
|
+
|
|
183
|
+
const legacy = await makeOutgoingRumor({ senderSigner, rumor })
|
|
184
|
+
if (preparedInnerEventsMatch(prepared, legacy.event)) return legacy
|
|
185
|
+
if (createdAtMs == null) throw new ValidationError('CREATED_AT_MS_REQUIRED')
|
|
186
|
+
throw new ValidationError('INVALID_PREPARED_PUBLICATION')
|
|
187
|
+
}
|
|
188
|
+
|
|
168
189
|
function assertValidSignedEvent (event) {
|
|
169
190
|
if (!isValidEvent(event)) {
|
|
170
191
|
throw new ValidationError('INVALID_SIGNED_EVENT')
|
|
@@ -744,6 +765,7 @@ export async function ask ({
|
|
|
744
765
|
|
|
745
766
|
const { event: question, wireEvent } = await makeOutgoingRumor({
|
|
746
767
|
senderSigner,
|
|
768
|
+
timestamped: true,
|
|
747
769
|
rumor: makeMessageRumor({
|
|
748
770
|
kind: ASK_KIND,
|
|
749
771
|
tags: [['r', receiverPubkey]],
|
|
@@ -788,6 +810,7 @@ export async function reply ({
|
|
|
788
810
|
if (!receiverPubkey) throw new ValidationError('RECEIVER_PUBKEY_REQUIRED')
|
|
789
811
|
const { event, wireEvent } = await makeOutgoingRumor({
|
|
790
812
|
senderSigner,
|
|
813
|
+
timestamped: true,
|
|
791
814
|
rumor: makeMessageRumor({
|
|
792
815
|
kind: REPLY_KIND,
|
|
793
816
|
tags: [['q', question.id], ['r', receiverPubkey]],
|
|
@@ -829,6 +852,7 @@ export async function tell ({
|
|
|
829
852
|
if (!receiverPubkey) throw new ValidationError('RECEIVER_PUBKEY_REQUIRED')
|
|
830
853
|
const { event, wireEvent } = await makeOutgoingRumor({
|
|
831
854
|
senderSigner,
|
|
855
|
+
timestamped: true,
|
|
832
856
|
rumor: makeMessageRumor({
|
|
833
857
|
kind: TELL_KIND,
|
|
834
858
|
tags: [['r', receiverPubkey]],
|
|
@@ -865,6 +889,7 @@ export async function yell ({
|
|
|
865
889
|
autoDeletionCapability = true,
|
|
866
890
|
fileChunkIndex,
|
|
867
891
|
createdAt,
|
|
892
|
+
createdAtMs,
|
|
868
893
|
onPreparedSeed,
|
|
869
894
|
preparedPublication,
|
|
870
895
|
preparedPublications,
|
|
@@ -882,9 +907,11 @@ export async function yell ({
|
|
|
882
907
|
idempotencyKey
|
|
883
908
|
})
|
|
884
909
|
if (createdAt !== undefined) rumor.created_at = createdAt
|
|
885
|
-
const { event, wireEvent } = await
|
|
910
|
+
const { event, wireEvent } = await makeYellRumor({
|
|
886
911
|
senderSigner,
|
|
887
|
-
rumor
|
|
912
|
+
rumor,
|
|
913
|
+
createdAtMs,
|
|
914
|
+
prepared
|
|
888
915
|
})
|
|
889
916
|
const deletion = resolveDeletionCapability({ deletionPubkey, deletionSeckey, autoDeletionCapability, preparedPublications: prepared })
|
|
890
917
|
const reports = await sendPrivateMessage({ senderSigner, imkcSigner, privateChannelSigner, privateChannelReaderPubkey, receivers, receiverTag: '', deletionPubkey: deletion.deletionPubkey, event: wireEvent, relays, relayToReceivers, recoveryRelays, expirationSeconds, useBulkPacing, temporaryStorageArea, fileChunkIndex, onPreparedSeed: preparedSeedHandler(onPreparedSeed, deletion), preparedPublications: prepared, _getIykcProofs, _publish })
|
|
@@ -72,6 +72,7 @@ import { DEFAULT_RECEIVED_CHUNK_TTL_MS } from '../private-channel/services/recei
|
|
|
72
72
|
import { createQueue } from '../idb-queue/index.js'
|
|
73
73
|
import { createChannelStateStore } from './services/channel-state.js'
|
|
74
74
|
import { normalizeIdempotencyKey, readIdempotencyKey } from '../private-message/helpers/idempotency.js'
|
|
75
|
+
import { eventCreatedAtMs, readMilliseconds } from '../private-message/helpers/timestamp.js'
|
|
75
76
|
import { DEFAULT_STALE_CHANNEL_SECONDS } from './constants/index.js'
|
|
76
77
|
import {
|
|
77
78
|
activatePrivateMessengerStorage,
|
|
@@ -130,6 +131,11 @@ const COALESCING_INDEX = 'byCoalescingKey'
|
|
|
130
131
|
const COALESCING_DIGEST = /^[0-9a-f]{64}$/
|
|
131
132
|
const COALESCING_KEEP = new Set(['oldest', 'latest'])
|
|
132
133
|
const DEDUPE_TYPES = new Set(['ask', 'reply', 'tell', 'yell'])
|
|
134
|
+
const TIMESTAMPED_PRIVATE_MESSAGE_KINDS = new Set([
|
|
135
|
+
privateMessage.ASK_KIND,
|
|
136
|
+
privateMessage.REPLY_KIND,
|
|
137
|
+
privateMessage.TELL_KIND
|
|
138
|
+
])
|
|
133
139
|
const MESSAGE_QUEUE_INDEXES = {
|
|
134
140
|
byChannel: 'channelPubkey',
|
|
135
141
|
[COALESCING_INDEX]: 'coalescingKey',
|
|
@@ -2218,13 +2224,30 @@ export class PrivateMessenger {
|
|
|
2218
2224
|
reason: tagged.reason
|
|
2219
2225
|
})
|
|
2220
2226
|
}
|
|
2227
|
+
const timestamped = TIMESTAMPED_PRIVATE_MESSAGE_KINDS.has(message.event?.kind)
|
|
2228
|
+
const milliseconds = timestamped
|
|
2229
|
+
? readMilliseconds(message.event)
|
|
2230
|
+
: { milliseconds: 0, reason: '' }
|
|
2231
|
+
if (milliseconds.reason && milliseconds.reason !== 'missing') {
|
|
2232
|
+
this.debug('milliseconds-tag-ignored', {
|
|
2233
|
+
type,
|
|
2234
|
+
code: messageCode(message) || '',
|
|
2235
|
+
reason: milliseconds.reason
|
|
2236
|
+
})
|
|
2237
|
+
}
|
|
2238
|
+
const messageCreatedAtMs = eventCreatedAtMs(message.event, {
|
|
2239
|
+
readMillisecondsTag: timestamped
|
|
2240
|
+
})
|
|
2221
2241
|
|
|
2222
2242
|
let resolved = null
|
|
2223
2243
|
if (this.resolveInboundCoalescing) {
|
|
2224
2244
|
try {
|
|
2225
2245
|
resolved = this.resolveInboundCoalescing(
|
|
2226
2246
|
{ ...message, type, channelPubkey },
|
|
2227
|
-
{
|
|
2247
|
+
{
|
|
2248
|
+
idempotencyKey: tagged.key,
|
|
2249
|
+
eventCreatedAtMs: messageCreatedAtMs
|
|
2250
|
+
}
|
|
2228
2251
|
)
|
|
2229
2252
|
if (resolved != null && typeof resolved?.then === 'function') {
|
|
2230
2253
|
throw new Error('COALESCING_RESOLVER_MUST_BE_SYNCHRONOUS')
|
|
@@ -2270,7 +2293,7 @@ export class PrivateMessenger {
|
|
|
2270
2293
|
digest
|
|
2271
2294
|
]),
|
|
2272
2295
|
coalescingKeep: keep,
|
|
2273
|
-
coalescingOrder: coalescingOrder(message, sequence)
|
|
2296
|
+
coalescingOrder: coalescingOrder(message, sequence, messageCreatedAtMs)
|
|
2274
2297
|
}
|
|
2275
2298
|
}
|
|
2276
2299
|
|
|
@@ -2749,7 +2772,7 @@ export class PrivateMessenger {
|
|
|
2749
2772
|
} finally { releaseActivity() }
|
|
2750
2773
|
}
|
|
2751
2774
|
|
|
2752
|
-
async yell ({ channelPubkey = this.defaultChannelPubkey(), receiverPubkeys, relays, relayToReceivers, message, code, payload, error, content, idempotencyKey, deletionPubkey, createdAt, onPreparedSeed, preparedPublication, preparedPublications, useBulkPacing = false }) {
|
|
2775
|
+
async yell ({ channelPubkey = this.defaultChannelPubkey(), receiverPubkeys, relays, relayToReceivers, message, code, payload, error, content, idempotencyKey, deletionPubkey, createdAt, createdAtMs, onPreparedSeed, preparedPublication, preparedPublications, useBulkPacing = false }) {
|
|
2753
2776
|
idempotencyKey = normalizeIdempotencyKey(idempotencyKey) || undefined
|
|
2754
2777
|
const channel = this.requireWritableChannel(channelPubkey)
|
|
2755
2778
|
const releaseActivity = this.decryptionScheduler.activity(channelPubkey)
|
|
@@ -2778,6 +2801,7 @@ export class PrivateMessenger {
|
|
|
2778
2801
|
content,
|
|
2779
2802
|
idempotencyKey,
|
|
2780
2803
|
createdAt,
|
|
2804
|
+
createdAtMs,
|
|
2781
2805
|
useBulkPacing,
|
|
2782
2806
|
onPreparedSeed: onPreparedSeed || this.outgoingSeedHandler(channelPubkey),
|
|
2783
2807
|
preparedPublications: prepared.length ? prepared : undefined,
|
|
@@ -3941,8 +3965,9 @@ function messageCode (message) {
|
|
|
3941
3965
|
: null
|
|
3942
3966
|
}
|
|
3943
3967
|
|
|
3944
|
-
function coalescingOrder (message, sequence = null) {
|
|
3968
|
+
function coalescingOrder (message, sequence = null, messageCreatedAtMs = 0) {
|
|
3945
3969
|
const order = {
|
|
3970
|
+
eventTimeMs: messageCreatedAtMs,
|
|
3946
3971
|
outer: Number.isSafeInteger(message?.outer?.created_at) ? message.outer.created_at : 0,
|
|
3947
3972
|
inner: Number.isSafeInteger(message?.event?.created_at) ? message.event.created_at : 0,
|
|
3948
3973
|
eventId: message?.event?.id || ''
|
|
@@ -3956,8 +3981,14 @@ function compareCoalescingOrder (left, right) {
|
|
|
3956
3981
|
left.sequence !== right.sequence) {
|
|
3957
3982
|
return left.sequence < right.sequence ? -1 : 1
|
|
3958
3983
|
}
|
|
3984
|
+
const leftTime = Number.isSafeInteger(left?.eventTimeMs)
|
|
3985
|
+
? left.eventTimeMs
|
|
3986
|
+
: (Number.isSafeInteger(left?.inner) ? left.inner * 1000 : 0)
|
|
3987
|
+
const rightTime = Number.isSafeInteger(right?.eventTimeMs)
|
|
3988
|
+
? right.eventTimeMs
|
|
3989
|
+
: (Number.isSafeInteger(right?.inner) ? right.inner * 1000 : 0)
|
|
3990
|
+
if (leftTime !== rightTime) return leftTime < rightTime ? -1 : 1
|
|
3959
3991
|
if ((left?.outer || 0) !== (right?.outer || 0)) return (left?.outer || 0) - (right?.outer || 0)
|
|
3960
|
-
if ((left?.inner || 0) !== (right?.inner || 0)) return (left?.inner || 0) - (right?.inner || 0)
|
|
3961
3992
|
const leftId = left?.eventId || ''
|
|
3962
3993
|
const rightId = right?.eventId || ''
|
|
3963
3994
|
if (leftId === rightId) return 0
|