libp2r2p 0.12.22 → 0.12.24
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 +19 -10
- package/idb-queue/index.js +86 -17
- package/package.json +1 -1
- package/private-messenger/index.js +133 -37
package/README.md
CHANGED
|
@@ -673,10 +673,13 @@ handleMessages().catch(err => console.warn('private messenger messages failed',
|
|
|
673
673
|
```
|
|
674
674
|
|
|
675
675
|
For one-at-a-time consumption, `await messenger.nextMessage()` returns
|
|
676
|
-
`{ message,
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
676
|
+
`{ message, progress, checkpoint, ownershipSignal, ack, nack, capacityEgress }`
|
|
677
|
+
or `null` if no record is currently available. `ack()` removes the reserved
|
|
678
|
+
record; `nack()` releases it for another attempt. Both return a boolean and are
|
|
679
|
+
idempotent for the same reservation. `checkpoint(value)` stores resumable
|
|
680
|
+
progress atomically with lease renewal. `ownershipSignal` aborts if the lease is
|
|
681
|
+
taken over. A superseded reservation returns `false`, cannot delete a later
|
|
682
|
+
delivery and exposes the safe reason through `lastFailure`.
|
|
680
683
|
|
|
681
684
|
Delivery leases last 30 seconds and renew every 10 seconds while held. Closing
|
|
682
685
|
the messenger or returning the iterator releases its unacknowledged deliveries;
|
|
@@ -1366,12 +1369,18 @@ Positional replacement and insertion follow the same rule: they reject instead
|
|
|
1366
1369
|
of evicting when the resulting queue would exceed the limit.
|
|
1367
1370
|
|
|
1368
1371
|
`queue.reserve({ leaseMs = 30000, nackCoalescingIndex, priorityIndex, priorities })` returns `null` or
|
|
1369
|
-
`{ item, ack, nack, renew }`. Reservation
|
|
1370
|
-
|
|
1371
|
-
`renew()` extends
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1372
|
+
`{ item, progress, ack, nack, renew, checkpoint, lastFailure }`. Reservation
|
|
1373
|
+
token/deadline and optional checkpoint progress are persisted atomically in the
|
|
1374
|
+
same IndexedDB transaction as selection. `renew()` extends ownership when the
|
|
1375
|
+
token still matches, even if a delayed timer runs just after `until`.
|
|
1376
|
+
`checkpoint(value)` atomically stores structured-clone progress, updates logical
|
|
1377
|
+
byte accounting and renews the lease. Progress survives NACK and a later
|
|
1378
|
+
reservation, but not replacement of the queued item. Low-level callers own
|
|
1379
|
+
renewal and release; PrivateMessenger supplies both. Do not mix reservations with
|
|
1380
|
+
destructive or position-shifting queue operations on the same queue except
|
|
1381
|
+
explicit removal. A stale token cannot acknowledge a newly reserved or replaced
|
|
1382
|
+
record; after a failed operation `lastFailure.code` distinguishes taken-over,
|
|
1383
|
+
missing and settled reservations.
|
|
1375
1384
|
`nackCoalescingIndex` names a non-unique index: when another matching item is
|
|
1376
1385
|
pending, NACK deletes the superseded active item instead of requeueing it.
|
|
1377
1386
|
`priorityIndex` names a scalar, non-multiEntry index and `priorities` is the
|
package/idb-queue/index.js
CHANGED
|
@@ -17,6 +17,8 @@ items, keyPath "position"
|
|
|
17
17
|
position integer ordering key between the queue head and tail
|
|
18
18
|
byteSize logical serialized size used by the queue capacity limit
|
|
19
19
|
item caller-provided queued value
|
|
20
|
+
progress optional structured-clone value checkpointed by a reservation
|
|
21
|
+
reservation optional temporary { token, until } ownership claim
|
|
20
22
|
|
|
21
23
|
items indexes
|
|
22
24
|
Declared by each createQueue() caller. A requested keyPath such as "status"
|
|
@@ -235,7 +237,7 @@ async function openQueueDatabase (indexedDB, prefix, indexDefinitions) {
|
|
|
235
237
|
throw new Error('QUEUE_SCHEMA_UPGRADE_FAILED')
|
|
236
238
|
}
|
|
237
239
|
|
|
238
|
-
function itemForStorage (position, item) {
|
|
240
|
+
function itemForStorage (position, item, progress) {
|
|
239
241
|
// IndexedDB does not expose portable per-record byte usage, so keep a stable
|
|
240
242
|
// JSON byte estimate for the queue's logical capacity limit.
|
|
241
243
|
// `position` is the ordering key; queued values remain caller payloads.
|
|
@@ -243,12 +245,21 @@ function itemForStorage (position, item) {
|
|
|
243
245
|
let byteSize = 0
|
|
244
246
|
let serialized = ''
|
|
245
247
|
while (true) {
|
|
246
|
-
serialized = JSON.stringify({
|
|
248
|
+
serialized = JSON.stringify({
|
|
249
|
+
byteSize,
|
|
250
|
+
item: storedItem,
|
|
251
|
+
...(progress === undefined ? {} : { progress })
|
|
252
|
+
})
|
|
247
253
|
const nextByteSize = byteLength(serialized)
|
|
248
254
|
if (nextByteSize === byteSize) break
|
|
249
255
|
byteSize = nextByteSize
|
|
250
256
|
}
|
|
251
|
-
return {
|
|
257
|
+
return {
|
|
258
|
+
position,
|
|
259
|
+
byteSize,
|
|
260
|
+
item: storedItem,
|
|
261
|
+
...(progress === undefined ? {} : { progress: structuredClone(progress) })
|
|
262
|
+
}
|
|
252
263
|
}
|
|
253
264
|
|
|
254
265
|
function assertIndex (index, length, { allowEnd = false } = {}) {
|
|
@@ -948,23 +959,67 @@ export async function createQueue ({
|
|
|
948
959
|
let settled = false
|
|
949
960
|
let completedAction
|
|
950
961
|
let completedResult
|
|
962
|
+
let lastFailure = null
|
|
951
963
|
let tail = Promise.resolve()
|
|
952
|
-
const update = (action,
|
|
953
|
-
|
|
964
|
+
const update = (action, {
|
|
965
|
+
now,
|
|
966
|
+
retryAfterMs = 0,
|
|
967
|
+
progress
|
|
968
|
+
} = {}) => {
|
|
969
|
+
const at = now === undefined ? Date.now() : now
|
|
970
|
+
if (!Number.isSafeInteger(at) || at < 0) {
|
|
971
|
+
return Promise.reject(new ValidationError('QUEUE_INVALID_TIME'))
|
|
972
|
+
}
|
|
973
|
+
if (action === 'nack' &&
|
|
974
|
+
(!Number.isSafeInteger(retryAfterMs) || retryAfterMs < 0 || retryAfterMs > 2147483647)) {
|
|
954
975
|
return Promise.reject(new ValidationError('QUEUE_INVALID_RETRY_AFTER'))
|
|
955
976
|
}
|
|
977
|
+
let checkpoint
|
|
978
|
+
if (action === 'checkpoint') {
|
|
979
|
+
if (progress === undefined) {
|
|
980
|
+
return Promise.reject(new ValidationError('QUEUE_CHECKPOINT_INVALID'))
|
|
981
|
+
}
|
|
982
|
+
try {
|
|
983
|
+
checkpoint = structuredClone(progress)
|
|
984
|
+
} catch {
|
|
985
|
+
return Promise.reject(new ValidationError('QUEUE_CHECKPOINT_INVALID'))
|
|
986
|
+
}
|
|
987
|
+
}
|
|
956
988
|
const operation = tail.then(async () => {
|
|
957
|
-
if (settled)
|
|
989
|
+
if (settled) {
|
|
990
|
+
if (action === completedAction) return completedResult
|
|
991
|
+
lastFailure = { code: 'QUEUE_RESERVATION_SETTLED' }
|
|
992
|
+
return false
|
|
993
|
+
}
|
|
994
|
+
lastFailure = null
|
|
958
995
|
const result = await mutate(async (tx, state) => {
|
|
959
996
|
const record = await getRecord(tx, reserved.position)
|
|
960
|
-
if (record
|
|
997
|
+
if (!record) return { ok: false, code: 'QUEUE_RESERVATION_MISSING' }
|
|
998
|
+
if (record.reservation?.token !== token) {
|
|
999
|
+
return { ok: false, code: 'QUEUE_RESERVATION_TAKEN_OVER' }
|
|
1000
|
+
}
|
|
961
1001
|
if (action === 'ack') {
|
|
962
1002
|
await deleteRecord(tx, record.position)
|
|
963
1003
|
state.usedBytes = Math.max(0, state.usedBytes - record.byteSize)
|
|
964
1004
|
await trimBounds(tx, state)
|
|
1005
|
+
} else if (action === 'checkpoint') {
|
|
1006
|
+
const stored = itemForStorage(record.position, record.item, checkpoint)
|
|
1007
|
+
if (hasByteLimit() && state.usedBytes - record.byteSize + stored.byteSize > sessionMaxBytes) {
|
|
1008
|
+
throw capacityError(stored.byteSize, state.usedBytes, sessionMaxBytes)
|
|
1009
|
+
}
|
|
1010
|
+
const previousByteSize = record.byteSize
|
|
1011
|
+
record.progress = checkpoint
|
|
1012
|
+
record.byteSize = stored.byteSize
|
|
1013
|
+
record.reservation = { token, until: at + leaseMs }
|
|
1014
|
+
await putRecord(tx, record)
|
|
1015
|
+
state.usedBytes = Math.max(0, state.usedBytes - previousByteSize + stored.byteSize)
|
|
1016
|
+
reserved.byteSize = stored.byteSize
|
|
1017
|
+
reserved.progress = structuredClone(checkpoint)
|
|
965
1018
|
} else {
|
|
966
|
-
if (action === 'renew')
|
|
967
|
-
|
|
1019
|
+
if (action === 'renew') {
|
|
1020
|
+
record.reservation.until = at + leaseMs
|
|
1021
|
+
await putRecord(tx, record)
|
|
1022
|
+
} else {
|
|
968
1023
|
let superseded = false
|
|
969
1024
|
if (nackDefinition) {
|
|
970
1025
|
try {
|
|
@@ -991,23 +1046,37 @@ export async function createQueue ({
|
|
|
991
1046
|
}
|
|
992
1047
|
}
|
|
993
1048
|
}
|
|
994
|
-
return true
|
|
995
|
-
}, { wakeWaiters: action !== 'renew' })
|
|
996
|
-
if (
|
|
1049
|
+
return { ok: true }
|
|
1050
|
+
}, { wakeWaiters: action !== 'renew' && action !== 'checkpoint' })
|
|
1051
|
+
if (!result.ok) {
|
|
1052
|
+
lastFailure = { code: result.code }
|
|
1053
|
+
settled = true
|
|
1054
|
+
completedAction = action
|
|
1055
|
+
completedResult = false
|
|
1056
|
+
return false
|
|
1057
|
+
}
|
|
1058
|
+
if (action !== 'renew' && action !== 'checkpoint') {
|
|
997
1059
|
settled = true
|
|
998
1060
|
completedAction = action
|
|
999
|
-
completedResult =
|
|
1061
|
+
completedResult = true
|
|
1000
1062
|
}
|
|
1001
|
-
return
|
|
1063
|
+
return true
|
|
1002
1064
|
})
|
|
1003
1065
|
tail = operation.catch(() => {})
|
|
1004
1066
|
return operation
|
|
1005
1067
|
}
|
|
1006
1068
|
return {
|
|
1007
1069
|
item: reserved.item,
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1070
|
+
get progress () {
|
|
1071
|
+
return reserved.progress === undefined ? undefined : structuredClone(reserved.progress)
|
|
1072
|
+
},
|
|
1073
|
+
get lastFailure () {
|
|
1074
|
+
return lastFailure == null ? null : { ...lastFailure }
|
|
1075
|
+
},
|
|
1076
|
+
ack: options => update('ack', options),
|
|
1077
|
+
nack: options => update('nack', options),
|
|
1078
|
+
renew: options => update('renew', options),
|
|
1079
|
+
checkpoint: (value, options = {}) => update('checkpoint', { ...options, progress: value })
|
|
1011
1080
|
}
|
|
1012
1081
|
}
|
|
1013
1082
|
|
package/package.json
CHANGED
|
@@ -38,8 +38,11 @@
|
|
|
38
38
|
// - Browser offline/online events add explicit offline ranges with a small skew.
|
|
39
39
|
// - Recovery defaults to 7 days and can be overridden per channel; zero disables durable recovery.
|
|
40
40
|
// - Channel state not actively leased or watched within the configured stale window is pruned.
|
|
41
|
-
// -
|
|
42
|
-
// -
|
|
41
|
+
// - The recovering asker reads relays first, then asks seeders for relay-uncovered intervals and the unproven left edge.
|
|
42
|
+
// - record.covered means relay/EOSE coverage; record.asked means an ask publication received at least one relay ACK.
|
|
43
|
+
// - Asked ranges are removed from persisted offlineRanges and advance askedThrough; no reply is awaited.
|
|
44
|
+
// - Priority/tail recovery uses only recently present seeders. Full recovery tries every configured seeder.
|
|
45
|
+
// - Seeder presence announcements occur every 10min; auto-discovered seeders are capped to the 8 most recently active.
|
|
43
46
|
// - Seeder/watchtower channels store reconstructed router events in a separate IndexedDB queue and auto-reply to recovery asks.
|
|
44
47
|
// - Seeder replies stream compact routers with createMissingMessageReplyPacker({ messenger, question }).update(seed), then finalize(optionalLastSeed).
|
|
45
48
|
// - For other event-list replies, use createEventReplyPacker({ messenger, question, code }).update(event).
|
|
@@ -196,6 +199,14 @@ function expiredCapacityEgress () {
|
|
|
196
199
|
})
|
|
197
200
|
}
|
|
198
201
|
|
|
202
|
+
function ownershipLostError (reason) {
|
|
203
|
+
return Object.assign(new Error('INBOX_DELIVERY_LOST'), {
|
|
204
|
+
code: 'INBOX_DELIVERY_LOST',
|
|
205
|
+
queueFailure: reason || '',
|
|
206
|
+
retryWhenAvailable: true
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
|
|
199
210
|
function isCapacityPressureError (error) {
|
|
200
211
|
return error?.code === 'PRIVATE_MESSENGER_CAPACITY_PRESSURE'
|
|
201
212
|
}
|
|
@@ -1227,6 +1238,8 @@ export class PrivateMessenger {
|
|
|
1227
1238
|
if (!this.offlineRecoverySecondsFor(pubkey)) return []
|
|
1228
1239
|
const channel = this.channels.get(pubkey)
|
|
1229
1240
|
const configuredSeeders = channel?.seeders || []
|
|
1241
|
+
// Full recovery intentionally attempts all configured seeders even when
|
|
1242
|
+
// recent presence has not been observed.
|
|
1230
1243
|
if (configuredSeeders.length) return configuredSeeders.filter(seeder => seeder !== this.userPubkey)
|
|
1231
1244
|
|
|
1232
1245
|
const activity = this.readState().channels[pubkey]?.seederActivity || {}
|
|
@@ -1663,7 +1676,7 @@ export class PrivateMessenger {
|
|
|
1663
1676
|
start: range.start,
|
|
1664
1677
|
end: range.end,
|
|
1665
1678
|
covered: [],
|
|
1666
|
-
|
|
1679
|
+
asked: [],
|
|
1667
1680
|
oldestCreatedAt: null,
|
|
1668
1681
|
relays: new Map(),
|
|
1669
1682
|
tier: '',
|
|
@@ -1736,6 +1749,12 @@ export class PrivateMessenger {
|
|
|
1736
1749
|
return false
|
|
1737
1750
|
}
|
|
1738
1751
|
|
|
1752
|
+
unhandledRecoveryRanges (record, range) {
|
|
1753
|
+
return record
|
|
1754
|
+
? subtractRanges(range, record.asked)
|
|
1755
|
+
: [range]
|
|
1756
|
+
}
|
|
1757
|
+
|
|
1739
1758
|
hasPendingRecovery (channelPubkey) {
|
|
1740
1759
|
return (this.readState().channels[channelPubkey]?.offlineRanges || []).length > 0
|
|
1741
1760
|
}
|
|
@@ -1836,7 +1855,7 @@ export class PrivateMessenger {
|
|
|
1836
1855
|
state.eose = done
|
|
1837
1856
|
state.pending = done
|
|
1838
1857
|
? []
|
|
1839
|
-
: subtractRanges({ start: record.start, end: record.end },
|
|
1858
|
+
: subtractRanges({ start: record.start, end: record.end }, this.recoveryCoverage(record, state))
|
|
1840
1859
|
state.events += entry.events || 0
|
|
1841
1860
|
if (Number.isFinite(entry.elapsedMs)) {
|
|
1842
1861
|
state.latencyMs = state.latencyMs == null
|
|
@@ -1863,17 +1882,51 @@ export class PrivateMessenger {
|
|
|
1863
1882
|
record.lastErrors = (history.relays || []).flatMap(entry => [...(entry.errors || []), ...(entry.error ? [entry.error] : [])])
|
|
1864
1883
|
}
|
|
1865
1884
|
|
|
1866
|
-
|
|
1885
|
+
recoveryCoverage (record, relayState = null) {
|
|
1886
|
+
return mergeRanges([
|
|
1887
|
+
...(relayState ? relayState.covered : record.covered),
|
|
1888
|
+
...record.asked
|
|
1889
|
+
])
|
|
1890
|
+
}
|
|
1891
|
+
|
|
1892
|
+
applyRelayCoverage (record, ranges) {
|
|
1867
1893
|
if (!ranges?.length) return
|
|
1868
|
-
record.claimed = mergeRanges([...record.claimed, ...ranges])
|
|
1869
1894
|
record.covered = mergeRanges([...record.covered, ...ranges])
|
|
1870
1895
|
for (const state of record.relays.values()) {
|
|
1871
1896
|
if (state.eose) continue
|
|
1872
|
-
state.pending = subtractRanges({ start: record.start, end: record.end },
|
|
1897
|
+
state.pending = subtractRanges({ start: record.start, end: record.end }, this.recoveryCoverage(record, state))
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1900
|
+
|
|
1901
|
+
async persistAskedCoverage (channelPubkey, ranges) {
|
|
1902
|
+
const state = this.readState()
|
|
1903
|
+
const current = state.channels[channelPubkey]
|
|
1904
|
+
if (!current || !ranges.length) return
|
|
1905
|
+
current.offlineRanges = mergeRanges((current.offlineRanges || [])
|
|
1906
|
+
.flatMap(range => subtractRanges(range, ranges)))
|
|
1907
|
+
current.askedThrough = Math.max(
|
|
1908
|
+
Number.isSafeInteger(current.askedThrough) ? current.askedThrough : 0,
|
|
1909
|
+
Math.max(...ranges.map(range => range.end))
|
|
1910
|
+
)
|
|
1911
|
+
state.channels[channelPubkey] = current
|
|
1912
|
+
await this.writeState(state)
|
|
1913
|
+
}
|
|
1914
|
+
|
|
1915
|
+
async applyAskedCoverage (record, ranges) {
|
|
1916
|
+
if (!ranges?.length) return
|
|
1917
|
+
record.asked = mergeRanges([...record.asked, ...ranges])
|
|
1918
|
+
for (const state of record.relays.values()) {
|
|
1919
|
+
if (state.eose) continue
|
|
1920
|
+
state.pending = subtractRanges(
|
|
1921
|
+
{ start: record.start, end: record.end },
|
|
1922
|
+
this.recoveryCoverage(record, state)
|
|
1923
|
+
)
|
|
1873
1924
|
}
|
|
1925
|
+
await this.persistAskedCoverage(record.channelPubkey, ranges)
|
|
1874
1926
|
}
|
|
1875
1927
|
|
|
1876
1928
|
activeRecoverySeeders (channelPubkey) {
|
|
1929
|
+
// Priority/tail recovery is the only path that requires recent presence.
|
|
1877
1930
|
if (!this.offlineRecoverySecondsFor(channelPubkey)) return []
|
|
1878
1931
|
const channel = this.channels.get(channelPubkey)
|
|
1879
1932
|
if (!channel?.signer) return []
|
|
@@ -1901,7 +1954,7 @@ export class PrivateMessenger {
|
|
|
1901
1954
|
)
|
|
1902
1955
|
if (!parts.length) continue
|
|
1903
1956
|
intersects = true
|
|
1904
|
-
if (subtractRanges(parts[0], record
|
|
1957
|
+
if (subtractRanges(parts[0], this.recoveryCoverage(record)).length) return true
|
|
1905
1958
|
}
|
|
1906
1959
|
return !intersects
|
|
1907
1960
|
})
|
|
@@ -1958,22 +2011,27 @@ export class PrivateMessenger {
|
|
|
1958
2011
|
async finalizeRecoveryRange ({ channelPubkey, range, record }) {
|
|
1959
2012
|
if (this.pauseReasons.size) throw this.pausedError()
|
|
1960
2013
|
if (!await this.checkRecoveryOnline()) return { complete: false, failures: [], offline: true }
|
|
1961
|
-
const uncovered = subtractRanges({ start: range.start, end: range.end }, record
|
|
2014
|
+
const uncovered = subtractRanges({ start: range.start, end: range.end }, this.recoveryCoverage(record))
|
|
1962
2015
|
const oldest = record.oldestCreatedAt
|
|
1963
|
-
const leftEdge = oldest
|
|
1964
|
-
? [{ start: range.start, end: range.end }]
|
|
1965
|
-
:
|
|
1966
|
-
? [{ start: range.start, end: Math.min(oldest, range.end) }]
|
|
1967
|
-
: []
|
|
2016
|
+
const leftEdge = oldest != null && oldest > range.start
|
|
2017
|
+
? [{ start: range.start, end: Math.min(oldest, range.end) }]
|
|
2018
|
+
: []
|
|
1968
2019
|
const seedRanges = mergeRanges([...uncovered, ...leftEdge])
|
|
2020
|
+
.flatMap(seedRange => subtractRanges(seedRange, record.asked))
|
|
1969
2021
|
const failures = []
|
|
2022
|
+
const remaining = []
|
|
1970
2023
|
for (const seedRange of seedRanges) {
|
|
1971
2024
|
if (this.pauseReasons.size) throw this.pausedError()
|
|
1972
2025
|
const attempt = await this.#askSeedersForMissingRangeAttempt(channelPubkey, seedRange.start, seedRange.end)
|
|
1973
2026
|
failures.push(...attempt.failures.map(failure => failure.error))
|
|
2027
|
+
if (!attempt.asks.length) {
|
|
2028
|
+
remaining.push(seedRange)
|
|
2029
|
+
continue
|
|
2030
|
+
}
|
|
2031
|
+
await this.applyAskedCoverage(record, [seedRange])
|
|
1974
2032
|
}
|
|
1975
2033
|
record.lastErrors = failures
|
|
1976
|
-
return { complete:
|
|
2034
|
+
return { complete: remaining.length === 0, failures, remaining }
|
|
1977
2035
|
}
|
|
1978
2036
|
|
|
1979
2037
|
scheduleLiveRecovery (pubkey) {
|
|
@@ -2514,6 +2572,7 @@ export class PrivateMessenger {
|
|
|
2514
2572
|
const messageCreatedAtMs = eventCreatedAtMs(message.event, {
|
|
2515
2573
|
readMillisecondsTag: timestamped
|
|
2516
2574
|
})
|
|
2575
|
+
const protocolResolved = missingMessagesAskCoalescingHint(message)
|
|
2517
2576
|
|
|
2518
2577
|
let resolved = null
|
|
2519
2578
|
if (this.resolveInboundCoalescing) {
|
|
@@ -2551,7 +2610,11 @@ export class PrivateMessenger {
|
|
|
2551
2610
|
this.onError?.(coalescingError(new ValidationError('INVALID_INBOUND_COALESCING_RESULT')))
|
|
2552
2611
|
}
|
|
2553
2612
|
}
|
|
2554
|
-
if (!digest &&
|
|
2613
|
+
if (!digest && protocolResolved) {
|
|
2614
|
+
digest = protocolResolved.key
|
|
2615
|
+
keep = protocolResolved.keep
|
|
2616
|
+
policy = 'protocol'
|
|
2617
|
+
} else if (!digest && tagged.key) {
|
|
2555
2618
|
digest = idempotencyDigest(tagged.key)
|
|
2556
2619
|
policy = 'idempotency'
|
|
2557
2620
|
}
|
|
@@ -2960,16 +3023,16 @@ export class PrivateMessenger {
|
|
|
2960
3023
|
const capacityEgress = this.createCapacityEgress()
|
|
2961
3024
|
let finish
|
|
2962
3025
|
const settled = new Promise(resolve => { finish = resolve })
|
|
2963
|
-
const done =
|
|
3026
|
+
const done = reason => {
|
|
2964
3027
|
clearInterval(timer)
|
|
2965
|
-
capacityEgress.release()
|
|
3028
|
+
capacityEgress.release(reason)
|
|
2966
3029
|
this.deliveries.delete(delivery)
|
|
2967
3030
|
finish()
|
|
2968
3031
|
this.wakeDeliveries()
|
|
2969
3032
|
}
|
|
2970
3033
|
const settle = async (method, options) => {
|
|
2971
3034
|
const result = await reservation[method](options)
|
|
2972
|
-
done()
|
|
3035
|
+
done(result ? undefined : ownershipLostError(reservation.lastFailure?.code))
|
|
2973
3036
|
if (method === 'ack' && result && !this.closePromise && this.pauseReasons.has('capacity')) {
|
|
2974
3037
|
this.checkCapacity().catch(err => this.onError?.(err))
|
|
2975
3038
|
}
|
|
@@ -2979,12 +3042,23 @@ export class PrivateMessenger {
|
|
|
2979
3042
|
message: withoutQueueMetadata(reservation.item),
|
|
2980
3043
|
ack: () => settle('ack'),
|
|
2981
3044
|
nack: options => settle('nack', options),
|
|
2982
|
-
|
|
3045
|
+
checkpoint: async progress => {
|
|
3046
|
+
const result = await reservation.checkpoint(progress)
|
|
3047
|
+
if (!result) done(ownershipLostError(reservation.lastFailure?.code))
|
|
3048
|
+
return result
|
|
3049
|
+
},
|
|
3050
|
+
capacityEgress: capacityEgress.messenger,
|
|
3051
|
+
ownershipSignal: capacityEgress.token.controller.signal,
|
|
3052
|
+
get progress () { return reservation.progress },
|
|
3053
|
+
get lastFailure () { return reservation.lastFailure }
|
|
2983
3054
|
}
|
|
2984
3055
|
Object.defineProperty(delivery, 'settled', { value: settled })
|
|
2985
3056
|
this.deliveries.add(delivery)
|
|
2986
3057
|
const timer = setInterval(() => {
|
|
2987
|
-
reservation.renew().then(
|
|
3058
|
+
reservation.renew().then(
|
|
3059
|
+
ok => { if (!ok) done(ownershipLostError(reservation.lastFailure?.code)) },
|
|
3060
|
+
err => { done(err); this.onError?.(err) }
|
|
3061
|
+
)
|
|
2988
3062
|
}, 10000)
|
|
2989
3063
|
timer.unref?.()
|
|
2990
3064
|
return delivery
|
|
@@ -3473,8 +3547,16 @@ export class PrivateMessenger {
|
|
|
3473
3547
|
if (!seconds) return
|
|
3474
3548
|
const now = nowSeconds()
|
|
3475
3549
|
const current = this.readState().channels[pubkey] || {}
|
|
3476
|
-
const checkpoint = Math.max(
|
|
3477
|
-
|
|
3550
|
+
const checkpoint = Math.max(
|
|
3551
|
+
current.lastSeenAt || 0,
|
|
3552
|
+
current.recoveredThrough || 0,
|
|
3553
|
+
current.askedThrough || 0
|
|
3554
|
+
)
|
|
3555
|
+
const since = current.askedThrough
|
|
3556
|
+
? Math.max(current.askedThrough + 1, checkpoint - this.offlineSkewSeconds)
|
|
3557
|
+
: checkpoint
|
|
3558
|
+
? checkpoint - this.offlineSkewSeconds
|
|
3559
|
+
: now - seconds
|
|
3478
3560
|
this.addOfflineRange(pubkey, Math.max(0, now - seconds, since), now)
|
|
3479
3561
|
}
|
|
3480
3562
|
|
|
@@ -3570,8 +3652,8 @@ export class PrivateMessenger {
|
|
|
3570
3652
|
},
|
|
3571
3653
|
useBulkPacing: priority !== 'normal'
|
|
3572
3654
|
})
|
|
3573
|
-
asks.push(ask)
|
|
3574
3655
|
await assertMessagePublished(ask, ask.question)
|
|
3656
|
+
asks.push(ask)
|
|
3575
3657
|
} catch (err) {
|
|
3576
3658
|
const error = sanitizePublicationError(err, {
|
|
3577
3659
|
operation: 'seeder-recovery-ask',
|
|
@@ -3852,7 +3934,7 @@ export class PrivateMessenger {
|
|
|
3852
3934
|
const seeders = this.activeRecoverySeeders(channelPubkey)
|
|
3853
3935
|
if (!seeders.length) return []
|
|
3854
3936
|
const failures = []
|
|
3855
|
-
const
|
|
3937
|
+
const asked = []
|
|
3856
3938
|
for (const interval of mergeRanges(intervals)) {
|
|
3857
3939
|
if (this.pauseReasons.size) throw this.pausedError()
|
|
3858
3940
|
const attempt = await this.#askSeedersForMissingRangeAttempt(channelPubkey, interval.start, interval.end, {
|
|
@@ -3860,13 +3942,13 @@ export class PrivateMessenger {
|
|
|
3860
3942
|
priority
|
|
3861
3943
|
})
|
|
3862
3944
|
failures.push(...attempt.failures.map(failure => failure.error))
|
|
3863
|
-
if (
|
|
3945
|
+
if (attempt.asks.length) asked.push(interval)
|
|
3864
3946
|
}
|
|
3865
|
-
if (
|
|
3947
|
+
if (asked.length) {
|
|
3866
3948
|
for (const record of this.recoveryRanges.values()) {
|
|
3867
3949
|
if (record.channelPubkey !== channelPubkey) continue
|
|
3868
|
-
const applied = intersectRanges(
|
|
3869
|
-
if (applied.length) this.
|
|
3950
|
+
const applied = intersectRanges(asked, [{ start: record.start, end: record.end }])
|
|
3951
|
+
if (applied.length) await this.applyAskedCoverage(record, applied)
|
|
3870
3952
|
}
|
|
3871
3953
|
}
|
|
3872
3954
|
return failures
|
|
@@ -3892,7 +3974,7 @@ export class PrivateMessenger {
|
|
|
3892
3974
|
[{ start: range.start, end: range.end }]
|
|
3893
3975
|
)
|
|
3894
3976
|
if (!priorityIntervals.length) continue
|
|
3895
|
-
const pending = subtractRanges({ start: range.start, end: range.end }, record
|
|
3977
|
+
const pending = subtractRanges({ start: range.start, end: range.end }, this.recoveryCoverage(record))
|
|
3896
3978
|
const wanted = intersectRanges(priorityIntervals, pending)
|
|
3897
3979
|
if (!wanted.length) continue
|
|
3898
3980
|
const candidates = this.orderedRecoveryRelays(record)
|
|
@@ -3909,13 +3991,13 @@ export class PrivateMessenger {
|
|
|
3909
3991
|
})
|
|
3910
3992
|
if (winner?.history) {
|
|
3911
3993
|
if (this.isPartialRecoveryHistory(winner.history)) this.updateRecoveryRecord(record, winner.history, { completeRelays: new Set() })
|
|
3912
|
-
else this.
|
|
3994
|
+
else this.applyRelayCoverage(record, wanted)
|
|
3913
3995
|
}
|
|
3914
3996
|
}
|
|
3915
3997
|
if (this.pauseReasons.size || this.closePromise) throw this.pausedError()
|
|
3916
3998
|
const currentUncovered = () => intersectRanges(
|
|
3917
3999
|
wanted,
|
|
3918
|
-
subtractRanges({ start: range.start, end: range.end }, record
|
|
4000
|
+
subtractRanges({ start: range.start, end: range.end }, this.recoveryCoverage(record))
|
|
3919
4001
|
)
|
|
3920
4002
|
const normalUncovered = intersectRanges(currentUncovered(), normalWindows)
|
|
3921
4003
|
if (normalUncovered.length) await this.askPrioritySeeders(channelPubkey, normalUncovered, { priority: 'normal' })
|
|
@@ -3993,7 +4075,7 @@ export class PrivateMessenger {
|
|
|
3993
4075
|
} else {
|
|
3994
4076
|
// Legacy fetch seam (tests/custom channels): resolves only when
|
|
3995
4077
|
// the whole range was read, so treat it as fully relay-covered.
|
|
3996
|
-
|
|
4078
|
+
this.applyRelayCoverage(record, [{ start: range.start, end: range.end }])
|
|
3997
4079
|
if (history?.oldestCreatedAt != null) {
|
|
3998
4080
|
record.oldestCreatedAt = record.oldestCreatedAt == null
|
|
3999
4081
|
? history.oldestCreatedAt
|
|
@@ -4010,11 +4092,11 @@ export class PrivateMessenger {
|
|
|
4010
4092
|
const lifecycleChanged = controller.signal.aborted || this.closePromise || !this.channels.has(pubkey) || !this.stopByChannel.has(pubkey) ||
|
|
4011
4093
|
(this.watchRevisionByChannel.get(pubkey) || 0) !== watchRevision
|
|
4012
4094
|
if (lifecycleChanged) {
|
|
4013
|
-
remaining.push(range)
|
|
4095
|
+
remaining.push(...this.unhandledRecoveryRanges(record, range))
|
|
4014
4096
|
continue
|
|
4015
4097
|
}
|
|
4016
4098
|
if (this.recoveryRecordActive(record)) {
|
|
4017
|
-
remaining.push(range)
|
|
4099
|
+
remaining.push(...this.unhandledRecoveryRanges(record, range))
|
|
4018
4100
|
continue
|
|
4019
4101
|
}
|
|
4020
4102
|
const recoveryStartedAt = this._now()
|
|
@@ -4036,7 +4118,7 @@ export class PrivateMessenger {
|
|
|
4036
4118
|
})
|
|
4037
4119
|
failures.push(...result.failures)
|
|
4038
4120
|
if (result.complete) recoveredThrough = Math.max(recoveredThrough, range.end)
|
|
4039
|
-
else remaining.push(range)
|
|
4121
|
+
else remaining.push(...(result.remaining || this.unhandledRecoveryRanges(record, range)))
|
|
4040
4122
|
} catch (err) {
|
|
4041
4123
|
const expectedPause = isExpectedRecoveryPause(err, controller)
|
|
4042
4124
|
const record = this.recoveryRanges.get(this.recoveryRangeKey(pubkey, range))
|
|
@@ -4045,7 +4127,7 @@ export class PrivateMessenger {
|
|
|
4045
4127
|
}
|
|
4046
4128
|
if (!expectedPause && !(controller.liveInterrupted && err === controller.signal.reason)) this.onError?.(err)
|
|
4047
4129
|
if (!expectedPause) failures.push(err)
|
|
4048
|
-
remaining.push(range)
|
|
4130
|
+
remaining.push(...this.unhandledRecoveryRanges(record, range))
|
|
4049
4131
|
} finally { this.recoveryControllers.delete(controller) }
|
|
4050
4132
|
}
|
|
4051
4133
|
const fresh = this.readState()
|
|
@@ -4520,6 +4602,20 @@ function idempotencyDigest (idempotencyKey) {
|
|
|
4520
4602
|
return coalescingDigest(['idempotency', idempotencyKey])
|
|
4521
4603
|
}
|
|
4522
4604
|
|
|
4605
|
+
function missingMessagesAskCoalescingHint (message) {
|
|
4606
|
+
if (messageCode(message) !== MISSING_MESSAGES_ASK_CODE) return null
|
|
4607
|
+
const payload = message?.payload?.payload
|
|
4608
|
+
const since = payload?.since
|
|
4609
|
+
const until = payload?.until
|
|
4610
|
+
if (!Number.isSafeInteger(since) || !Number.isSafeInteger(until) || until < since) return null
|
|
4611
|
+
const priority = payload.priority == null ? 'normal' : payload.priority
|
|
4612
|
+
if (!['normal', 'lower', 'lowest'].includes(priority)) return null
|
|
4613
|
+
return {
|
|
4614
|
+
key: coalescingDigest(['missing-messages-ask', since, until, priority]),
|
|
4615
|
+
keep: 'latest'
|
|
4616
|
+
}
|
|
4617
|
+
}
|
|
4618
|
+
|
|
4523
4619
|
function coalescingDigest (value) {
|
|
4524
4620
|
return bytesToHex(sha256(encoder.encode(JSON.stringify(value))))
|
|
4525
4621
|
}
|