hyperjoin 0.0.0-stage → 1.4.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.
package/lib/peer.js ADDED
@@ -0,0 +1,264 @@
1
+ const unslab = require('unslab')
2
+ const b4a = require('b4a')
3
+
4
+ const DIAL_BACKOFF_MAX = 10 * 60 * 1000
5
+ const DIAL_ATTEMPTS_RESET = 60 * 1000
6
+ const HAMMERING_THRESHOLD = 10 * 1000
7
+
8
+ const BAN_DURATION = 1000 * 60 * 30
9
+ const BAN_HAMMER_ATTEMPTS = 20
10
+ const HAMMER_WINDOW = 10 * 60 * 1000 // short disconnects older than this are forgiven
11
+
12
+ const ERR_SUSPEND = 'SuspendDrop'
13
+
14
+ // legacy announce-refresh is 10(+2) minutes
15
+ // https://github.com/holepunchto/hyperannounce/blob/main/index.js#L7-L9
16
+ const EXPIRE_DURATION = 1000 * 60 * 15 // 15 min
17
+
18
+ class Peer {
19
+ publicKey = null
20
+ relayAddresses = []
21
+ forceRelaying = false
22
+
23
+ seenCount = 0
24
+
25
+ lastSeenAt = -1
26
+ attemptedAt = -1
27
+ bannedAt = -1
28
+ banReason = null
29
+ connectedAt = -1
30
+ disconnectedAt = -1
31
+ scannedAt = -1 // last seen by a discovering session
32
+
33
+ timeConnected = 0
34
+ _connectedCount = 0
35
+ _disconnectedCount = 0
36
+ _shortDisconnects = [] // recent short inbound disconnects
37
+
38
+ _topics = new Map() // hex => seenAt
39
+
40
+ _outboundAttempts = 0
41
+ _outboundSuccess = 0
42
+ _outboundFailures = 0
43
+ _inboundAttempts = 0
44
+ _inboundSuccess = 0
45
+ _inboundFailures = 0
46
+
47
+ #created = Date.now()
48
+
49
+ // dialer owned
50
+ _refreshedAt = -1
51
+ _banReported = false
52
+
53
+ constructor(publicKey) {
54
+ this.publicKey = unslab(publicKey)
55
+ }
56
+
57
+ shouldDial(now = Date.now()) {
58
+ if (this.isBanned) return false
59
+
60
+ // peer is not announcing
61
+ if (this.lastSeenAt === -1) return false
62
+
63
+ if (this.isDialing) return false
64
+
65
+ if (this.isExpired(now)) return false
66
+
67
+ return this.retryAt <= now
68
+ }
69
+
70
+ get topics() {
71
+ return Array.from(this._topics.keys())
72
+ }
73
+
74
+ get isBanned() {
75
+ return this.bannedAt > 0
76
+ }
77
+
78
+ setBanned(reason = 'banned', now = Date.now()) {
79
+ this.bannedAt = now
80
+ this.banReason = reason
81
+
82
+ // clear memory
83
+ this._shortDisconnects = []
84
+ this.relayAddresses = []
85
+ this._topics.clear()
86
+ }
87
+
88
+ get expiresAt() {
89
+ if (this.isBanned) {
90
+ return this.bannedAt + BAN_DURATION
91
+ }
92
+
93
+ const lastActive = Math.max(this.lastSeenAt, this.disconnectedAt)
94
+
95
+ return lastActive + EXPIRE_DURATION
96
+ }
97
+
98
+ isExpired(now = Date.now()) {
99
+ if (this.connected) return false
100
+
101
+ // In the unlikely event of an upsert w/o seen()
102
+ if (!this.isBanned && this.lastSeenAt === -1 && this.disconnectedAt === -1) {
103
+ return Date.now() - this.#created > 10_000
104
+ }
105
+
106
+ return now > this.expiresAt
107
+ }
108
+
109
+ get connected() {
110
+ return this._connectedCount > this._disconnectedCount
111
+ }
112
+
113
+ get isDialing() {
114
+ return this._isAttempting
115
+ }
116
+
117
+ get retryAt() {
118
+ if (!this._outboundAttempts) return this.lastSeenAt
119
+
120
+ const backoff = Math.min(Peer.backoffDuration(this._outboundAttempts), DIAL_BACKOFF_MAX)
121
+
122
+ return this.attemptedAt + backoff
123
+ }
124
+
125
+ get _nextAt() {
126
+ return Math.min(this.retryAt, this.expiresAt)
127
+ }
128
+
129
+ observe(topic, now = Date.now()) {
130
+ this.seenCount++
131
+ this.lastSeenAt = now
132
+
133
+ const hex = b4a.toString(topic, 'hex')
134
+ this._topics.set(hex, now)
135
+ }
136
+
137
+ updateRelays(relayAddresses) {
138
+ this.relayAddresses = relayAddresses
139
+ }
140
+
141
+ // TODO: rework dial-attempt api
142
+ markAttempt(initiator, { canRelay = false, now = Date.now() } = {}) {
143
+ const prevAttemptAt = this.attemptedAt
144
+ this.attemptedAt = now
145
+ this._isAttempting = true
146
+
147
+ if (initiator) {
148
+ this._outboundAttempts++
149
+ } else {
150
+ this._inboundAttempts++
151
+ }
152
+
153
+ let done = false
154
+ return (failed = false, cause = null, now = Date.now()) => {
155
+ if (done) throw new Error('assert fail: attempt already settled')
156
+ done = true
157
+ this._isAttempting = false
158
+ this.attemptedAt = now // update
159
+
160
+ if (failed) {
161
+ // @since v0.5: cause normalized to 'string'
162
+ cause = cause?.code || cause
163
+
164
+ if (initiator) {
165
+ this._outboundFailures++
166
+
167
+ if (cause === ERR_SUSPEND) {
168
+ if (this._outboundAttempts > 0) this._outboundAttempts--
169
+ this._refreshedAt = -1
170
+ this.attemptedAt = prevAttemptAt
171
+ } else if (canRelay && !this.forceRelaying && shouldForceRelaying(cause)) {
172
+ this.forceRelaying = true
173
+ this._outboundAttempts = 0 // fastconnect to relay
174
+
175
+ // reschedule
176
+ this._refreshedAt = -1
177
+ // this.lastSeenAt++
178
+ }
179
+ } else {
180
+ this._inboundFailures++
181
+ }
182
+
183
+ // Process bans-preconnect
184
+ const reason = this.shouldBan(initiator, cause, { prevAttemptAt })
185
+ if (reason) {
186
+ this.setBanned(reason, now)
187
+ }
188
+ } else {
189
+ if (initiator) {
190
+ this._outboundSuccess++
191
+ } else {
192
+ this._inboundSuccess++
193
+ }
194
+ }
195
+ }
196
+ }
197
+
198
+ resetAttempts() {
199
+ this._outboundAttempts = 0
200
+ this._inboundAttempts = 0
201
+ this._refreshedAt = -1 // let refresh() re-process
202
+ }
203
+
204
+ markConnected(initiator, now = Date.now()) {
205
+ this.connectedAt = now
206
+ this._connectedCount++
207
+ }
208
+
209
+ markDisconnected(initiator, cause, now = Date.now()) {
210
+ // @since v0.5: cause normalized to 'string'
211
+ cause = cause?.code || cause
212
+
213
+ this._disconnectedCount++
214
+ const delta = now - this.connectedAt
215
+ this.timeConnected += delta
216
+ this.disconnectedAt = now
217
+ if (initiator) this.lastSeenAt = now
218
+
219
+ if (!initiator && delta < HAMMERING_THRESHOLD) {
220
+ this._shortDisconnects = this._shortDisconnects.filter((t) => t > now - HAMMER_WINDOW)
221
+ this._shortDisconnects.push(now)
222
+ }
223
+
224
+ // Process bans-postconnect
225
+ const reason = this.shouldBan(initiator, cause)
226
+ if (reason) {
227
+ this.setBanned(reason, now)
228
+ }
229
+
230
+ // connection survived gc,
231
+ // reset attempts after 60s connected time
232
+ if (delta > DIAL_ATTEMPTS_RESET) {
233
+ this._outboundAttempts = 0
234
+ this._inboundAttempts = 0
235
+ }
236
+ }
237
+
238
+ shouldBan(initiator, cause /* opts = {} */) {
239
+ if (initiator) {
240
+ // outbound
241
+ } else {
242
+ // inbound
243
+
244
+ if (this._shortDisconnects.length >= BAN_HAMMER_ATTEMPTS) return 'hammering'
245
+ }
246
+
247
+ return null
248
+ }
249
+
250
+ static backoffDuration(nAttempts) {
251
+ // 1s, 2.5s, 6s, 15s, 36s ...
252
+ return 1000 * Math.exp(nAttempts * 0.9027)
253
+ }
254
+ }
255
+
256
+ function shouldForceRelaying(code) {
257
+ return (
258
+ code === 'HOLEPUNCH_ABORTED' ||
259
+ code === 'HOLEPUNCH_DOUBLE_RANDOMIZED_NATS' ||
260
+ code === 'REMOTE_NOT_HOLEPUNCHABLE'
261
+ )
262
+ }
263
+
264
+ module.exports = Peer
package/lib/session.js ADDED
@@ -0,0 +1,355 @@
1
+ const ReadyResource = require('ready-resource')
2
+ const Discovery = require('hyperannounce')
3
+ const b4a = require('b4a')
4
+
5
+ const INACTIVE = 'inactive'
6
+ const PINNED = 'pinned'
7
+ const AUTOSTARTED = 'autostarted'
8
+ const DISCOVERING = 'discovering'
9
+ const DESTROYED = 'destroyed'
10
+
11
+ module.exports = class ReplicatorSession extends ReadyResource {
12
+ constructor(replicator, store, topic, opts = {}) {
13
+ super()
14
+ this.replicator = replicator
15
+ this.store = store // instance of Replicator.root.session()
16
+ this.topic = topic
17
+ this.client = opts.client !== false // Currently unused
18
+ this.server = opts.server !== false
19
+
20
+ this._onpeer = this._onpeer.bind(this)
21
+ this._onbump = this._onbump.bind(this)
22
+ this._onstoreclose = this.close.bind(this)
23
+ this.store.on('close', this._onstoreclose)
24
+ this._onstats = this._onstats.bind(this)
25
+
26
+ this.discovery = null
27
+ this._opts = opts
28
+
29
+ this._deferSwarming = opts.deferSwarming !== false
30
+
31
+ this.state = INACTIVE
32
+ this._suspended = replicator.suspended
33
+
34
+ this.priority = opts.priority || 0
35
+ this._startDelay = 0
36
+ this._startTime = 0
37
+ this._stopTime = 0
38
+ this._starts = 0
39
+ this._timeActive = 0
40
+ this._discoveryTimeout = 0
41
+ this._forceRefresh = false
42
+
43
+ if (opts.pin) this.pin()
44
+ else if (opts.discover) this.discover()
45
+ }
46
+
47
+ async _open() {
48
+ const { replicator, topic } = this
49
+ await replicator.ready()
50
+
51
+ const { suspended, keyPair, _dhtFastRetry: fastRetry } = replicator
52
+
53
+ this.discovery = new Discovery(replicator.dht, topic, {
54
+ announce: false,
55
+ suspended,
56
+ keyPair,
57
+ relayAddresses: () => replicator.server.relayAddresses,
58
+ onpeer: this._onpeer,
59
+ onbump: this._onbump,
60
+ onstats: this._onstats,
61
+ fastRetry,
62
+ ...this._opts
63
+ })
64
+ }
65
+
66
+ get isPinned() {
67
+ return this.state === PINNED
68
+ }
69
+ get isDiscovering() {
70
+ return this.state === DISCOVERING
71
+ }
72
+ get isAuto() {
73
+ return this.state === AUTOSTARTED
74
+ }
75
+ get active() {
76
+ return this.state !== INACTIVE && this.state !== DESTROYED
77
+ }
78
+ get announce() {
79
+ return !!(this.discovery && this.discovery.announce)
80
+ }
81
+ get isAnnounced() {
82
+ return !!(this.discovery && this.discovery._needsUnannounce)
83
+ }
84
+
85
+ hasCore(dkey) {
86
+ const core = this.store.cores.get(dkey)
87
+ if (!core) return false
88
+
89
+ // Check if store session has core session
90
+ const sessions = this.store.sessions.list(core.id)
91
+ return sessions.length > 0
92
+ }
93
+
94
+ _transition(target) {
95
+ const prev = this.state
96
+ this.state = target
97
+ this.emit('state', target, prev)
98
+ if (prev === DISCOVERING && target !== DISCOVERING) this.emit('discovered')
99
+ }
100
+
101
+ _start(mode, now = Date.now()) {
102
+ if (this.state !== INACTIVE) throw new Error('Invalid state: ' + this.state)
103
+
104
+ this._startTime = now
105
+ this._stopTime = 0
106
+ this._starts++
107
+ this._startDelay = 0
108
+
109
+ const { _autoStartupDelay, _defaultDiscoveryTimeout, _discoveryStartupDelay } = this.replicator
110
+
111
+ switch (mode) {
112
+ case AUTOSTARTED:
113
+ this._startDelay = this._deferSwarming ? approximateFuture(_autoStartupDelay) : 0
114
+ break
115
+
116
+ case DISCOVERING:
117
+ this._discoveryTimeout = _defaultDiscoveryTimeout
118
+ this._startDelay = this._deferSwarming ? approximateFuture(_discoveryStartupDelay) : 0
119
+ break
120
+
121
+ case PINNED:
122
+ break
123
+
124
+ default:
125
+ throw new Error(`Unknown state: ${mode}`)
126
+ }
127
+
128
+ this._transition(mode)
129
+
130
+ this.emit('activate', this)
131
+ this.replicator.emit('activate', this)
132
+ }
133
+
134
+ _stop(now = Date.now()) {
135
+ if (this.state === INACTIVE) return // no-op
136
+
137
+ this._stopTime = now
138
+ this._timeActive += now - this._startTime
139
+ this._startTime = 0
140
+ this._startDelay = 0
141
+
142
+ switch (this.state) {
143
+ case DISCOVERING:
144
+ this._discoveryTimeout = 0
145
+ this.discovery.abort()
146
+ break
147
+ }
148
+
149
+ this._transition(INACTIVE)
150
+
151
+ this.emit('deactivate', this)
152
+ this.replicator.emit('deactivate', this)
153
+ }
154
+
155
+ pin() {
156
+ if (this.isPinned) return
157
+
158
+ if (this.isAuto || this.isDiscovering) this._stop() // upgrade
159
+
160
+ this._start(PINNED)
161
+ }
162
+
163
+ unpin() {
164
+ if (!this.isPinned) return
165
+ this._stop()
166
+ }
167
+
168
+ _autoStart(now = Date.now()) {
169
+ if (this.isAuto) return
170
+ this._start(AUTOSTARTED, now)
171
+ }
172
+
173
+ _autoStop(now = Date.now()) {
174
+ if (!this.isAuto) return
175
+ this._stop(now)
176
+ }
177
+
178
+ discover() {
179
+ if (this.isDiscovering) return
180
+
181
+ if (this.isPinned /* || this.isAuto */) {
182
+ this._forceRefresh = true
183
+ return
184
+ }
185
+
186
+ if (this.isAuto) {
187
+ this._stop() // degrade into discovering
188
+ }
189
+
190
+ this._start(DISCOVERING)
191
+ }
192
+
193
+ _stopDiscovery(now = Date.now()) {
194
+ if (!this.isDiscovering) return
195
+ this._stop(now)
196
+ }
197
+
198
+ bump(value = -1) {
199
+ if (value !== -1) {
200
+ this.discovery.update(value)
201
+ }
202
+
203
+ // adjust discovery timeout
204
+ if (this.isDiscovering) {
205
+ const { _updatedDiscoveryTimeout } = this.replicator
206
+ this._startTime = Date.now() // reset
207
+ this._discoveryTimeout = _updatedDiscoveryTimeout
208
+ }
209
+ }
210
+
211
+ get seen() {
212
+ if (!this.discovery) return -1
213
+ return this.discovery.bump.seen
214
+ }
215
+
216
+ async refresh(now = Date.now()) {
217
+ await this.ready() // <-- wait root corestore keygen
218
+
219
+ if (this.isDiscovering) {
220
+ const expired = now - this._startTime > this._discoveryTimeout
221
+ if (expired) this._stopDiscovery(now)
222
+ }
223
+
224
+ const startDelayed = now - this._startTime >= this._startDelay
225
+
226
+ const autostartActive = this.isAuto && startDelayed
227
+ const queryRunnerActive = this.discovery.active
228
+
229
+ // announce
230
+
231
+ const announcing = this.discovery.announce
232
+
233
+ let shouldAnnounce = false
234
+ shouldAnnounce ||= this.isPinned
235
+ shouldAnnounce ||= autostartActive
236
+ shouldAnnounce &&= this.server
237
+ shouldAnnounce &&= !this.replicator.isPresuspended
238
+ shouldAnnounce &&= this.replicator.listening
239
+
240
+ this.discovery.setAnnounce(shouldAnnounce) // sets Dirty & aborts query if active
241
+
242
+ // TODO: move to `replicator-discovery`
243
+ // refresh
244
+
245
+ let doRefresh = false
246
+
247
+ doRefresh ||= announcing !== shouldAnnounce // un-announce
248
+ doRefresh ||= this.isDiscovering && !queryRunnerActive && startDelayed
249
+
250
+ // bootstrap a (pinned/autostarted) `server: false` session
251
+ doRefresh ||=
252
+ (this.isPinned || autostartActive) &&
253
+ !this.server &&
254
+ !queryRunnerActive &&
255
+ this.discovery.lastRefreshed === -1 &&
256
+ !this.replicator.isPresuspended &&
257
+ this.replicator.listening
258
+
259
+ doRefresh ||= this._forceRefresh
260
+ this._forceRefresh = false
261
+
262
+ if (doRefresh) {
263
+ this.discovery.refresh()
264
+ }
265
+ }
266
+
267
+ async flush() {
268
+ await this.ready()
269
+ await this.discovery.flush()
270
+ }
271
+
272
+ _shouldEvict(now = Date.now()) {
273
+ if (this.active) return false
274
+
275
+ // applies to "pending" (_starts === 0)
276
+ if (this._stopTime === 0) {
277
+ this._stopTime = now
278
+ }
279
+
280
+ const idleDuration = now - this._stopTime
281
+ const { _gcIdleTime } = this.replicator
282
+
283
+ if (idleDuration < _gcIdleTime) return false // retain in pool
284
+
285
+ return true // expired
286
+ }
287
+
288
+ _onpeer(peer) {
289
+ if (b4a.equals(peer.publicKey, this.replicator.peerId)) {
290
+ return // filter self
291
+ }
292
+
293
+ const info = this.replicator.dialer.seen(peer, this.topic)
294
+ if (this.isDiscovering) info.scannedAt = Date.now()
295
+ }
296
+
297
+ _onbump(value) {
298
+ this.emit('bump', value)
299
+ }
300
+
301
+ _onstats(_, action) {
302
+ switch (action) {
303
+ case 'lookup':
304
+ this.replicator.stats.dht.lookup++
305
+ break
306
+
307
+ case 'announce':
308
+ this.replicator.stats.dht.announce++
309
+ break
310
+
311
+ case 'unannounce':
312
+ this.replicator.stats.dht.unannounce++
313
+ break
314
+ }
315
+ }
316
+
317
+ async suspend({ log = noop } = {}) {
318
+ if (this._suspended) return
319
+ this._suspended = true
320
+
321
+ await this.ready()
322
+
323
+ // Ensure previous session promise is resolved
324
+ if (this._opts.wait) await this._opts.wait
325
+ await this.discovery.suspend({ log })
326
+ }
327
+
328
+ async resume({ log = noop } = {}) {
329
+ await this.ready()
330
+ this.discovery.resume({ log })
331
+ this._suspended = false
332
+ }
333
+
334
+ async _close() {
335
+ this.store.removeListener('close', this._onstoreclose)
336
+ this._stop()
337
+ this._transition(DESTROYED)
338
+
339
+ // Ensure previous session promise is resolved
340
+ if (this._opts.wait) await this._opts.wait
341
+ if (this.discovery) await this.discovery.destroy()
342
+ }
343
+
344
+ // lunte-disable-next-line require-await
345
+ async destroy() {
346
+ return this.close()
347
+ }
348
+ }
349
+
350
+ function approximateFuture(delay) {
351
+ const jitter = Math.floor(Math.random() * 0.5 * delay)
352
+ return delay + jitter
353
+ }
354
+
355
+ function noop() {}
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "hyperjoin",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.4.0",
4
+ "description": "replicator",
5
+ "main": "index.js",
6
+ "exports": {
7
+ "./package": "./package.json",
8
+ ".": {
9
+ "types": "./index.d.ts",
10
+ "default": "./index.js"
11
+ }
12
+ },
13
+ "imports": {
14
+ "events": {
15
+ "bare": "bare-events",
16
+ "default": "events"
17
+ }
18
+ },
19
+ "files": [
20
+ "package.json",
21
+ "index.js",
22
+ "index.d.ts",
23
+ "schema",
24
+ "lib"
25
+ ],
26
+ "devDependencies": {
27
+ "brittle": "^3.19.1",
28
+ "corestore": "^7.14.0",
29
+ "hyperdht": "^6.34.1",
30
+ "lunte": "^1.8.6",
31
+ "prettier": "^3.9.10",
32
+ "prettier-config-holepunch": "^2.0.0"
33
+ },
34
+ "scripts": {
35
+ "format": "prettier . --write",
36
+ "test": "brittle-bare test/index.js",
37
+ "lint": "prettier . --check && lunte"
38
+ },
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/holepunchto/hyperjoin.git"
42
+ },
43
+ "author": "Holepunch Inc",
44
+ "license": "Apache-2.0",
45
+ "bugs": {
46
+ "url": "https://github.com/holepunchto/hyperjoin/issues"
47
+ },
48
+ "homepage": "https://github.com/holepunchto/hyperjoin",
49
+ "dependencies": {
50
+ "b4a": "^1.9.0",
51
+ "bare-events": "^2.9.2",
52
+ "hypercore": "^11.39.0",
53
+ "ready-resource": "^1.2.0",
54
+ "hyperannounce": "^1.3.1",
55
+ "safety-catch": "^1.0.3"
56
+ }
57
+ }