blind-peer 3.8.2 → 3.10.0

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.
Files changed (2) hide show
  1. package/index.js +87 -5
  2. package/package.json +7 -5
package/index.js CHANGED
@@ -24,6 +24,8 @@ const {
24
24
  RouterResolvePeersRequest,
25
25
  RouterResolvePeersResponse
26
26
  } = require('blind-peer-encodings')
27
+ const blindPush = require('blind-push')
28
+ const { ForwardPushRequest } = require('blind-push/encodings')
27
29
 
28
30
  const BlindPeerDB = require('./lib/db.js')
29
31
  const TopKWindow = require('./lib/top-k.js')
@@ -218,19 +220,22 @@ class BlindPeer extends ReadyResource {
218
220
  routerPoolOpts,
219
221
  ipBanListKeys = [],
220
222
  banTimeout = 16_000,
223
+ pushGatewayKeys,
224
+ pushGatewayPoolOpts,
221
225
  port,
222
226
  bootstrap = null,
223
227
  announcingInterval = 100,
224
228
  wakeupGcTickTime = null,
225
229
  replicationLagThreshold = 100,
226
230
  topK = {},
227
- adminRouter = null
231
+ adminRouter = null,
232
+ activeCorestore = false
228
233
  } = {}
229
234
  ) {
230
235
  super()
231
236
 
232
237
  this.rocks = typeof rocks === 'string' ? new RocksDB(rocks) : rocks
233
- this.store = store || new Corestore(this.rocks, { active: false })
238
+ this.store = store || new Corestore(this.rocks, { active: activeCorestore })
234
239
  this.swarm = swarm || null
235
240
  const ipBanNs = this.store.namespace('ip-ban-lists')
236
241
  this.ipBanLists = ipBanListKeys.map((key) => new IpBanList(ipBanNs, { key }))
@@ -256,10 +261,13 @@ class BlindPeer extends ReadyResource {
256
261
  this.announcedCores = new Map()
257
262
  this.replicationLagThreshold = replicationLagThreshold
258
263
 
264
+ this.rpcClient = null
259
265
  this.routerKey = routerKey || null
260
266
  this.routerPoolOpts = routerPoolOpts || {}
261
267
  this.routerPool = null
262
268
  this.adminRouter = adminRouter
269
+ this.pushGatewayKeys = pushGatewayKeys || []
270
+ this.pushGatewayPoolOpts = pushGatewayPoolOpts || {}
263
271
 
264
272
  this.stats = {
265
273
  bytesGcd: 0,
@@ -267,6 +275,9 @@ class BlindPeer extends ReadyResource {
267
275
  activations: 0,
268
276
  wakeups: 0,
269
277
  addCoresRx: 0,
278
+ notificationsRx: 0,
279
+ notificationsSent: 0,
280
+ notificationErrors: 0,
270
281
  muxerPaired: 0,
271
282
  muxerErrors: 0
272
283
  }
@@ -364,9 +375,20 @@ class BlindPeer extends ReadyResource {
364
375
  })
365
376
  )
366
377
 
378
+ this.rpcClient = new ProtomuxRpcClient(this.swarm.dht)
367
379
  if (this.routerKey) {
368
- const rpcClient = new ProtomuxRpcClient(this.swarm.dht)
369
- this.routerPool = new ProtomuxRpcClientPool([this.routerKey], rpcClient, this.routerPoolOpts)
380
+ this.routerPool = new ProtomuxRpcClientPool(
381
+ [this.routerKey],
382
+ this.rpcClient,
383
+ this.routerPoolOpts
384
+ )
385
+ }
386
+ if (this.pushGatewayKeys.length) {
387
+ this.gatewayPool = new ProtomuxRpcClientPool(
388
+ this.pushGatewayKeys,
389
+ this.rpcClient,
390
+ this.pushGatewayPoolOpts
391
+ )
370
392
  }
371
393
 
372
394
  await this.topKByPeer.ready()
@@ -536,6 +558,15 @@ class BlindPeer extends ReadyResource {
536
558
  self.emit('muxer-error', e, conn)
537
559
  throw e
538
560
  }
561
+ },
562
+ async onnotification(request) {
563
+ try {
564
+ await self._onnotification(conn, request)
565
+ } catch (e) {
566
+ self.stats.notificationErrors++
567
+ self.emit('notification-error', e, conn)
568
+ throw e
569
+ }
539
570
  }
540
571
  })
541
572
  })
@@ -877,11 +908,46 @@ class BlindPeer extends ReadyResource {
877
908
  return true
878
909
  }
879
910
 
911
+ async _onnotification(stream, request) {
912
+ this.stats.notificationsRx++
913
+
914
+ if (!this.gatewayPool) {
915
+ return null
916
+ }
917
+
918
+ const core = this.store.get({ key: request.block.key })
919
+
920
+ await core.ready()
921
+
922
+ const payload = await blindPush.createNotification(core, {
923
+ roomKey: request.destination.key,
924
+ roomDiscoveryKey: request.destination.discoveryKey,
925
+ index: request.block.index,
926
+ version: request.version,
927
+ extra: request.extra
928
+ })
929
+
930
+ await this.gatewayPool.makeRequest(
931
+ 'forward-push',
932
+ { payload, appId: request.appId },
933
+ {
934
+ requestEncoding: ForwardPushRequest,
935
+ responseEncoding: c.none
936
+ }
937
+ )
938
+
939
+ this.stats.notificationsSent++
940
+ this.emit('notification-sent', request, payload, stream)
941
+ }
942
+
880
943
  async _close() {
881
944
  if (this.routerPool) {
882
945
  await this.routerPool.destroy()
883
- await this.routerPool.statelessRpc.close()
884
946
  }
947
+ if (this.gatewayPool) {
948
+ await this.gatewayPool.destroy()
949
+ }
950
+ await this.rpcClient.close()
885
951
  if (this.adminRouter) await this.adminRouter.close()
886
952
  clearInterval(this.flushInterval)
887
953
  await this.topKByPeer.close()
@@ -1065,6 +1131,22 @@ class BlindPeer extends ReadyResource {
1065
1131
  this.set(self.rocks.stats.writeBatches)
1066
1132
  }
1067
1133
  })
1134
+
1135
+ new promClient.Gauge({
1136
+ name: 'blind_peer_corestore_active',
1137
+ help: 'Whether the corestore is active (1) or passive (0)',
1138
+ collect() {
1139
+ this.set(self.store.active ? 1 : 0)
1140
+ }
1141
+ })
1142
+
1143
+ new promClient.Gauge({
1144
+ name: 'blind_peer_push_notifications_active',
1145
+ help: 'Whether push notifications can be forwarded (1) or not (0)',
1146
+ collect() {
1147
+ this.set(self.gatewayPool ? 1 : 0)
1148
+ }
1149
+ })
1068
1150
  }
1069
1151
  }
1070
1152
  }
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "blind-peer",
3
- "version": "3.8.2",
3
+ "version": "3.10.0",
4
4
  "description": "Blind peers help keep hypercores available",
5
5
  "main": "index.js",
6
6
  "dependencies": {
7
7
  "autobase": "^7.0.18",
8
8
  "b4a": "^1.6.7",
9
9
  "blind-peer-encodings": "^3.3.0",
10
- "blind-peer-muxer": "^1.0.0",
10
+ "blind-peer-muxer": "^1.3.0",
11
+ "blind-push": "^0.2.5",
11
12
  "compact-encoding": "^3.0.0",
12
13
  "corestore": "^7.4.4",
13
14
  "hypercore": "^11.26.0",
@@ -19,8 +20,8 @@
19
20
  "hyperswarm": "^4.13.1",
20
21
  "ip-ban-list": "^0.3.0",
21
22
  "protomux-rpc": "^1.7.1",
22
- "protomux-rpc-client": "^2.1.0",
23
- "protomux-rpc-client-pool": "^2.1.0",
23
+ "protomux-rpc-client": "^2.3.0",
24
+ "protomux-rpc-client-pool": "^2.2.0",
24
25
  "protomux-wakeup": "^2.9.0",
25
26
  "ready-resource": "^1.1.2",
26
27
  "repl-swarm": "^2.3.0",
@@ -34,7 +35,8 @@
34
35
  "bare-process": "^4.2.2",
35
36
  "bare-prom-client": "^15.1.3",
36
37
  "blind-peer-router": "^0.2.2",
37
- "blind-peering": "^2.1.2",
38
+ "blind-peering": "^2.2.1",
39
+ "blind-push-gateway": "^0.1.1",
38
40
  "brittle": "^3.7.0",
39
41
  "debounceify": "^1.1.0",
40
42
  "graceful-goodbye": "^1.3.3",