@mblaney/holster 1.0.18 → 1.0.20
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/build/holster.js +2 -2
- package/build/holster.js.map +2 -2
- package/package.json +1 -1
- package/src/wire.js +41 -34
package/package.json
CHANGED
package/src/wire.js
CHANGED
|
@@ -15,7 +15,7 @@ if (typeof globalThis.WebSocket === "undefined") {
|
|
|
15
15
|
// Rate limiting with throttling
|
|
16
16
|
const createRateLimiter = isTestEnv => {
|
|
17
17
|
const clients = new Map()
|
|
18
|
-
const maxRequests =
|
|
18
|
+
const maxRequests = 1000 // requests per minute
|
|
19
19
|
const windowMs = 60000 // 1 minute window
|
|
20
20
|
const disconnectThreshold = 10 // Disconnect after 10 violations
|
|
21
21
|
let cleanupInterval = null
|
|
@@ -264,20 +264,27 @@ const Wire = opt => {
|
|
|
264
264
|
const put = async (msg, send) => {
|
|
265
265
|
// Store updates returned from Ham.mix and defer updates if required.
|
|
266
266
|
const update = await Ham.mix(msg.put, graph, opt.secure, listen)
|
|
267
|
-
if (Object.keys(update.now).length) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
"#": dup.track(utils.text.random(9)),
|
|
274
|
-
"@": msg["#"],
|
|
275
|
-
err: err,
|
|
276
|
-
}),
|
|
277
|
-
)
|
|
278
|
-
})
|
|
267
|
+
if (Object.keys(update.now).length === 0) {
|
|
268
|
+
// No updates to store, check deferred.
|
|
269
|
+
if (Object.keys(update.defer).length !== 0) {
|
|
270
|
+
setTimeout(() => put({put: update.defer}, send), update.wait)
|
|
271
|
+
}
|
|
272
|
+
return
|
|
279
273
|
}
|
|
280
|
-
|
|
274
|
+
|
|
275
|
+
if (!(await check(update.now, send))) return
|
|
276
|
+
|
|
277
|
+
store.put(update.now, err => {
|
|
278
|
+
send(
|
|
279
|
+
JSON.stringify({
|
|
280
|
+
"#": dup.track(utils.text.random(9)),
|
|
281
|
+
"@": msg["#"],
|
|
282
|
+
err: err,
|
|
283
|
+
}),
|
|
284
|
+
)
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
if (Object.keys(update.defer).length !== 0) {
|
|
281
288
|
setTimeout(() => put({put: update.defer}, send), update.wait)
|
|
282
289
|
}
|
|
283
290
|
}
|
|
@@ -353,29 +360,29 @@ const Wire = opt => {
|
|
|
353
360
|
// used whereas wire spec needs to handle clock skew for updates
|
|
354
361
|
// across the network.
|
|
355
362
|
const update = await Ham.mix(data, graph, opt.secure, listen)
|
|
363
|
+
if (Object.keys(update.now).length === 0) {
|
|
364
|
+
// No updates, still respond to callback.
|
|
365
|
+
if (cb) cb(null)
|
|
366
|
+
return
|
|
367
|
+
}
|
|
356
368
|
|
|
357
|
-
if (
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
pendingReferences.add(soulId)
|
|
368
|
-
}
|
|
369
|
+
if (!(await check(update.now, send, cb))) return
|
|
370
|
+
|
|
371
|
+
// Seed pendingReferences with any new references from API calls
|
|
372
|
+
for (const [soul, node] of Object.entries(update.now)) {
|
|
373
|
+
if (node && typeof node === "object") {
|
|
374
|
+
for (const [key, value] of Object.entries(node)) {
|
|
375
|
+
const soulId = utils.rel.is(value)
|
|
376
|
+
if (soulId) {
|
|
377
|
+
// Add referenced soul to pending list
|
|
378
|
+
pendingReferences.add(soulId)
|
|
369
379
|
}
|
|
370
380
|
}
|
|
371
381
|
}
|
|
372
|
-
|
|
373
|
-
store.put(update.now, cb)
|
|
374
|
-
} else {
|
|
375
|
-
// No local update needed, but still callback
|
|
376
|
-
if (cb) cb(null)
|
|
377
382
|
}
|
|
378
383
|
|
|
384
|
+
store.put(update.now, cb)
|
|
385
|
+
|
|
379
386
|
// Always put data on the wire spec even if no local update needed
|
|
380
387
|
const sendResult = send(
|
|
381
388
|
JSON.stringify({
|
|
@@ -640,8 +647,8 @@ const Wire = opt => {
|
|
|
640
647
|
|
|
641
648
|
// Schedule processing of next message if queue not empty
|
|
642
649
|
if (messageQueue.length > 0) {
|
|
643
|
-
// Add delay between messages to respect rate
|
|
644
|
-
queueProcessor = setTimeout(processQueue,
|
|
650
|
+
// Add delay between messages to respect rate limit
|
|
651
|
+
queueProcessor = setTimeout(processQueue, 100)
|
|
645
652
|
} else {
|
|
646
653
|
queueProcessor = null
|
|
647
654
|
}
|
|
@@ -685,7 +692,7 @@ const Wire = opt => {
|
|
|
685
692
|
messageQueue.push(data)
|
|
686
693
|
// Start queue processor if not already running
|
|
687
694
|
if (!queueProcessor) {
|
|
688
|
-
queueProcessor = setTimeout(processQueue,
|
|
695
|
+
queueProcessor = setTimeout(processQueue, 100)
|
|
689
696
|
}
|
|
690
697
|
}
|
|
691
698
|
if (!(opt.peers instanceof Array)) {
|