@edryslabs/genericprovider 1.0.3 → 1.0.4
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/dist/index.d.ts +140 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +402 -113
- package/dist/index.js.map +1 -1
- package/dist/providers/gun/index.d.ts +18 -0
- package/dist/providers/gun/index.d.ts.map +1 -1
- package/dist/providers/gun/index.js +61 -12
- package/dist/providers/gun/index.js.map +1 -1
- package/dist/providers/matrix/index.d.ts +6 -0
- package/dist/providers/matrix/index.d.ts.map +1 -1
- package/dist/providers/matrix/index.js +6 -0
- package/dist/providers/matrix/index.js.map +1 -1
- package/dist/providers/nostr/index.d.ts +7 -0
- package/dist/providers/nostr/index.d.ts.map +1 -1
- package/dist/providers/nostr/index.js +7 -0
- package/dist/providers/nostr/index.js.map +1 -1
- package/dist/providers/simple-peer/index.d.ts +29 -0
- package/dist/providers/simple-peer/index.d.ts.map +1 -1
- package/dist/providers/simple-peer/index.js +93 -12
- package/dist/providers/simple-peer/index.js.map +1 -1
- package/dist/transport.d.ts +11 -0
- package/dist/transport.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -100,15 +100,20 @@ export declare class GenericProvider extends Observable<string> {
|
|
|
100
100
|
private _bcChannel;
|
|
101
101
|
private _bcConnected;
|
|
102
102
|
private _bcSubscriber?;
|
|
103
|
-
private
|
|
104
|
-
private
|
|
103
|
+
private _resyncAttemptCount;
|
|
104
|
+
private _lastResyncAttemptTime;
|
|
105
|
+
private _pendingResyncTimeoutId?;
|
|
105
106
|
private _syncRequestTimes;
|
|
106
107
|
private _maxSyncRequestsPerWindow;
|
|
107
108
|
private _syncRequestWindowMs;
|
|
109
|
+
private _pendingSyncReply;
|
|
110
|
+
private _pendingSyncReplyTimeoutId?;
|
|
111
|
+
private _syncReplySuppressionMs;
|
|
108
112
|
private _localSeqNum;
|
|
109
|
-
private
|
|
110
|
-
private
|
|
111
|
-
private
|
|
113
|
+
private _remoteSeqInfo;
|
|
114
|
+
private _gapCheckTimers;
|
|
115
|
+
private _seqWindowSize;
|
|
116
|
+
private _gapGraceMs;
|
|
112
117
|
private _batchUpdates;
|
|
113
118
|
private _pendingUpdate;
|
|
114
119
|
private _batchTimeoutId?;
|
|
@@ -154,7 +159,8 @@ export declare class GenericProvider extends Observable<string> {
|
|
|
154
159
|
* Updates are collected and sent after this delay in milliseconds.
|
|
155
160
|
* Set to 0 to send updates immediately (no batching).
|
|
156
161
|
* Recommended: 50-200ms for good balance between latency and efficiency.
|
|
157
|
-
* @default
|
|
162
|
+
* @default the transport's `preferredBatchMs` hint if it declares one,
|
|
163
|
+
* otherwise 0 (disabled - immediate transmission)
|
|
158
164
|
*/
|
|
159
165
|
batchUpdates?: number;
|
|
160
166
|
/**
|
|
@@ -197,6 +203,47 @@ export declare class GenericProvider extends Observable<string> {
|
|
|
197
203
|
* @default 'push-pull'
|
|
198
204
|
*/
|
|
199
205
|
syncMode?: 'push-pull' | 'pull';
|
|
206
|
+
/**
|
|
207
|
+
* Max number of sync requests (SyncStep1 pulls and syncNow() pushes
|
|
208
|
+
* combined) this provider will send within `syncRequestWindowMs`.
|
|
209
|
+
* Protects against self-inflicted resync storms (e.g. many hash
|
|
210
|
+
* mismatches firing in a short window under packet loss). Raise this
|
|
211
|
+
* if legitimate resyncs are being throttled under heavy loss; lower
|
|
212
|
+
* it to bound worst-case traffic more aggressively per peer.
|
|
213
|
+
* @default 20
|
|
214
|
+
*/
|
|
215
|
+
maxSyncRequestsPerWindow?: number;
|
|
216
|
+
/**
|
|
217
|
+
* Rolling time window (ms) over which `maxSyncRequestsPerWindow` is
|
|
218
|
+
* enforced.
|
|
219
|
+
* @default 10000
|
|
220
|
+
*/
|
|
221
|
+
syncRequestWindowMs?: number;
|
|
222
|
+
/**
|
|
223
|
+
* Max random delay (ms) before replying to a SyncStep1 request, used
|
|
224
|
+
* to let other peers' replies pre-empt a redundant one (NACK-style
|
|
225
|
+
* suppression). Only engages once at least 2 other peers are known via
|
|
226
|
+
* awareness. Larger values suppress more redundant traffic in large
|
|
227
|
+
* rooms at the cost of higher requester-perceived latency.
|
|
228
|
+
* @default 30
|
|
229
|
+
*/
|
|
230
|
+
syncReplySuppressionMs?: number;
|
|
231
|
+
/**
|
|
232
|
+
* Grace period (ms) after detecting a suspected sequence-number gap
|
|
233
|
+
* before requesting a resync. Tolerates mere network reordering
|
|
234
|
+
* without treating it as loss; lower it to detect genuine packet loss
|
|
235
|
+
* faster at the risk of more false-positive resyncs under jitter.
|
|
236
|
+
* @default 300
|
|
237
|
+
*/
|
|
238
|
+
gapGraceMs?: number;
|
|
239
|
+
/**
|
|
240
|
+
* Number of recent sequence numbers retained per remote peer for
|
|
241
|
+
* duplicate/gap detection. Raise if a transport can deliver messages
|
|
242
|
+
* extremely out of order across a wide window; the default is ample
|
|
243
|
+
* for typical reordering/jitter.
|
|
244
|
+
* @default 64
|
|
245
|
+
*/
|
|
246
|
+
seqWindowSize?: number;
|
|
200
247
|
});
|
|
201
248
|
/**
|
|
202
249
|
* Connect to the backend and start syncing.
|
|
@@ -257,6 +304,93 @@ export declare class GenericProvider extends Observable<string> {
|
|
|
257
304
|
* Corrupt messages are rejected immediately without attempting to decode.
|
|
258
305
|
*/
|
|
259
306
|
private _handleIncomingMessage;
|
|
307
|
+
/**
|
|
308
|
+
* Schedule a SyncStep2 reply after a short random delay instead of
|
|
309
|
+
* sending immediately. If another peer's reply is overheard in the
|
|
310
|
+
* meantime (`_cancelPendingSyncReply`), this reply is dropped as
|
|
311
|
+
* redundant - the requester likely already got what it needed.
|
|
312
|
+
*
|
|
313
|
+
* A reply that is already pending when this is called answers a
|
|
314
|
+
* *different* SyncStep1 request (e.g. peer A's request, followed 5ms
|
|
315
|
+
* later by peer B's) - it must not be silently overwritten by the new
|
|
316
|
+
* one. Flush it immediately, then schedule the new reply fresh. The only
|
|
317
|
+
* sanctioned way a reply gets dropped is `_cancelPendingSyncReply()`,
|
|
318
|
+
* because we overheard someone else's SyncStep2 for the SAME request.
|
|
319
|
+
*/
|
|
320
|
+
private _scheduleSyncReply;
|
|
321
|
+
/** Cancel a pending suppressed reply, if any. */
|
|
322
|
+
private _cancelPendingSyncReply;
|
|
323
|
+
/**
|
|
324
|
+
* Send a SyncStep2 reply, gated by the same shared per-peer budget as
|
|
325
|
+
* SyncStep1 requests/syncNow() pushes (`_tryReserveSyncSlot()`).
|
|
326
|
+
*
|
|
327
|
+
* Previously SyncStep2 replies were completely unrated - the only
|
|
328
|
+
* defense against redundant replies was the best-effort NACK-style
|
|
329
|
+
* suppression in `_scheduleSyncReply()`/`_cancelPendingSyncReply()`,
|
|
330
|
+
* which itself is just an ordinary broadcast message subject to the same
|
|
331
|
+
* wire corruption as everything else. Under sustained corruption, more
|
|
332
|
+
* competing repliers independently miss the "someone already answered"
|
|
333
|
+
* signal as peer count grows, and none of that traffic was bounded.
|
|
334
|
+
* Measured in test/dummy/bench-corruption-storm.ts: SyncStep2/SyncStep1
|
|
335
|
+
* ratio grew from ~1.1-1.3 at N=2 to ~4.5-5.9 at N=10 (should stay near
|
|
336
|
+
* 1 if suppression alone were sufficient). This is a hard backstop on
|
|
337
|
+
* top of that suppression, not a replacement for it - a rate-limited
|
|
338
|
+
* reply is dropped silently (no warn) since under normal, uncorrupted
|
|
339
|
+
* operation this path is rarely exercised and logging every drop here
|
|
340
|
+
* would itself become log spam exactly when things are already noisy.
|
|
341
|
+
*/
|
|
342
|
+
private _sendSyncReply;
|
|
343
|
+
/**
|
|
344
|
+
* Track a received sequence number for reordering-tolerant gap detection.
|
|
345
|
+
* Does not gate whether the update gets applied — only decides whether a
|
|
346
|
+
* gap looks suspicious enough to (eventually) request a resync.
|
|
347
|
+
*/
|
|
348
|
+
private _trackRemoteSeq;
|
|
349
|
+
/**
|
|
350
|
+
* Re-check a suspected sequence gap after a short grace period instead of
|
|
351
|
+
* requesting a resync immediately. Pure network reordering (a message
|
|
352
|
+
* that's merely late, not lost) typically resolves itself within the
|
|
353
|
+
* grace window, so this avoids the resync storms that immediate gap
|
|
354
|
+
* detection caused under jitter. Real packet loss still gets caught —
|
|
355
|
+
* just `_gapGraceMs` later — and the periodic sync interval / hash
|
|
356
|
+
* verification remain as further safety nets regardless.
|
|
357
|
+
*/
|
|
358
|
+
private _scheduleGapCheck;
|
|
359
|
+
/**
|
|
360
|
+
* Unified entry point for ALL resync triggers (hash mismatch, corrupted
|
|
361
|
+
* message, confirmed sequence gap). Coalesces them behind a single
|
|
362
|
+
* pending timer and a single shared escalation counter, so a burst of
|
|
363
|
+
* triggers from different causes in a short window schedules exactly one
|
|
364
|
+
* resync instead of three independent ones each able to draw on the
|
|
365
|
+
* shared `_tryReserveSyncSlot()` budget on their own.
|
|
366
|
+
*
|
|
367
|
+
* Always resolves to `syncNow()` (push + pull) rather than distinguishing
|
|
368
|
+
* a push-only/pull-only variant per trigger. `syncNow()`'s push half is
|
|
369
|
+
* already a no-op when there's nothing to send (it only calls
|
|
370
|
+
* `_sendUpdate()` when `update.length > 0`), so unifying on push+pull is
|
|
371
|
+
* strictly simpler than threading a `push` flag through a *shared*
|
|
372
|
+
* coordinator (where the "right" answer for an absorbed trigger is
|
|
373
|
+
* ambiguous anyway - was it push-worthy or not?). It also closes a latent
|
|
374
|
+
* gap where the corrupted-message and gap-confirmed triggers previously
|
|
375
|
+
* called pull-only `_sendSyncStep1()` and could never deliver this peer's
|
|
376
|
+
* own surplus edits made during a divergence window.
|
|
377
|
+
*/
|
|
378
|
+
private _requestResync;
|
|
379
|
+
/**
|
|
380
|
+
* Reserve a slot in the sync rate limiter (max `_maxSyncRequestsPerWindow`
|
|
381
|
+
* per `_syncRequestWindowMs`), recording the request if there's room.
|
|
382
|
+
* Shared by `_sendSyncStep1()` and `syncNow()` so a burst of triggers from
|
|
383
|
+
* different sources (periodic sync, hash-mismatch resyncs, gap-check
|
|
384
|
+
* confirmations) draws from one combined budget instead of each having
|
|
385
|
+
* its own uncapped or separately-capped allowance.
|
|
386
|
+
*/
|
|
387
|
+
private _tryReserveSyncSlot;
|
|
388
|
+
/**
|
|
389
|
+
* Encode and send a SyncStep1 message requesting missing updates.
|
|
390
|
+
* Does not check the rate limiter itself - callers must reserve a slot
|
|
391
|
+
* via `_tryReserveSyncSlot()` first.
|
|
392
|
+
*/
|
|
393
|
+
private _writeSyncStep1;
|
|
260
394
|
/**
|
|
261
395
|
* Send SyncStep1 message to request missing updates.
|
|
262
396
|
* This is sent when first connecting to sync with remote peers.
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAA;AACxB,OAAO,KAAK,iBAAiB,MAAM,uBAAuB,CAAA;AAI1D,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAG5C,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAA;AACxB,OAAO,KAAK,iBAAiB,MAAM,uBAAuB,CAAA;AAI1D,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAG5C,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAyHhF;;;GAGG;AACH,qBAAa,aAAc,SAAQ,UAAU,CAAC,MAAM,CAAC;IACnD,OAAO,CAAC,QAAQ,CAAiB;gBAErB,QAAQ,EAAE,eAAe;IAKrC;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,GAAG,IAAI;IAI1C;;;;;;;;;;OAUG;IACH,SAAS,CAAC,MAAM,EAAE,MAA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