libp2p 0.43.4 → 0.44.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/dist/index.min.js +30 -30
- package/dist/src/autonat/index.d.ts +1 -2
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +4 -6
- package/dist/src/autonat/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
- package/dist/src/circuit-relay/utils.d.ts.map +1 -1
- package/dist/src/circuit-relay/utils.js +3 -1
- package/dist/src/circuit-relay/utils.js.map +1 -1
- package/dist/src/components.d.ts +1 -6
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +0 -10
- package/dist/src/components.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +0 -9
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +36 -0
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -0
- package/dist/src/connection-manager/auto-dial.js +122 -0
- package/dist/src/connection-manager/auto-dial.js.map +1 -0
- package/dist/src/connection-manager/connection-pruner.d.ts +28 -0
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -0
- package/dist/src/connection-manager/connection-pruner.js +96 -0
- package/dist/src/connection-manager/connection-pruner.js.map +1 -0
- package/dist/src/connection-manager/constants.d.ts +49 -0
- package/dist/src/connection-manager/constants.d.ts.map +1 -0
- package/dist/src/connection-manager/constants.js +49 -0
- package/dist/src/connection-manager/constants.js.map +1 -0
- package/dist/src/connection-manager/dial-queue.d.ts +77 -0
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -0
- package/dist/src/connection-manager/dial-queue.js +366 -0
- package/dist/src/connection-manager/dial-queue.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +65 -69
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +116 -234
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.d.ts +9 -0
- package/dist/src/connection-manager/utils.d.ts.map +1 -0
- package/dist/src/connection-manager/utils.js +66 -0
- package/dist/src/connection-manager/utils.js.map +1 -0
- package/dist/src/fetch/index.js +3 -3
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/identify/index.js +5 -5
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js +3 -1
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +10 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +7 -13
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/upgrader.d.ts +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +2 -1
- package/dist/src/upgrader.js.map +1 -1
- package/dist/typedoc-urls.json +1 -1
- package/package.json +18 -18
- package/src/autonat/index.ts +6 -9
- package/src/circuit-relay/transport/reservation-store.ts +1 -1
- package/src/circuit-relay/utils.ts +3 -1
- package/src/components.ts +1 -17
- package/src/config.ts +0 -9
- package/src/connection-manager/auto-dial.ts +171 -0
- package/src/connection-manager/connection-pruner.ts +137 -0
- package/src/connection-manager/constants.ts +60 -0
- package/src/connection-manager/dial-queue.ts +504 -0
- package/src/connection-manager/index.ts +190 -323
- package/src/connection-manager/utils.ts +76 -0
- package/src/fetch/index.ts +4 -4
- package/src/identify/index.ts +5 -5
- package/src/insecure/index.ts +5 -1
- package/src/libp2p.ts +17 -15
- package/src/upgrader.ts +3 -2
- package/src/version.ts +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts +0 -42
- package/dist/src/connection-manager/auto-dialler.d.ts.map +0 -1
- package/dist/src/connection-manager/auto-dialler.js +0 -105
- package/dist/src/connection-manager/auto-dialler.js.map +0 -1
- package/dist/src/connection-manager/dialer/dial-request.d.ts +0 -27
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/dial-request.js +0 -114
- package/dist/src/connection-manager/dialer/dial-request.js.map +0 -1
- package/dist/src/connection-manager/dialer/index.d.ts +0 -121
- package/dist/src/connection-manager/dialer/index.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/index.js +0 -324
- package/dist/src/connection-manager/dialer/index.js.map +0 -1
- package/dist/src/connection-manager/latency-monitor.d.ts +0 -120
- package/dist/src/connection-manager/latency-monitor.d.ts.map +0 -1
- package/dist/src/connection-manager/latency-monitor.js +0 -220
- package/dist/src/connection-manager/latency-monitor.js.map +0 -1
- package/dist/src/connection-manager/visibility-change-emitter.d.ts +0 -66
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +0 -1
- package/dist/src/connection-manager/visibility-change-emitter.js +0 -118
- package/dist/src/connection-manager/visibility-change-emitter.js.map +0 -1
- package/dist/src/constants.d.ts +0 -27
- package/dist/src/constants.d.ts.map +0 -1
- package/dist/src/constants.js +0 -31
- package/dist/src/constants.js.map +0 -1
- package/src/connection-manager/auto-dialler.ts +0 -158
- package/src/connection-manager/dialer/dial-request.ts +0 -144
- package/src/connection-manager/dialer/index.ts +0 -461
- package/src/connection-manager/latency-monitor.ts +0 -319
- package/src/connection-manager/visibility-change-emitter.ts +0 -135
- package/src/constants.ts +0 -36
|
@@ -1,319 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This code is based on `latency-monitor` (https://github.com/mlucool/latency-monitor) by `mlucool` (https://github.com/mlucool), available under Apache License 2.0 (https://github.com/mlucool/latency-monitor/blob/master/LICENSE)
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
6
|
-
import { VisibilityChangeEmitter } from './visibility-change-emitter.js'
|
|
7
|
-
import { logger } from '@libp2p/logger'
|
|
8
|
-
|
|
9
|
-
const log = logger('libp2p:connection-manager:latency-monitor')
|
|
10
|
-
|
|
11
|
-
export interface LatencyMonitorEvents {
|
|
12
|
-
'data': CustomEvent<SummaryObject>
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface LatencyMonitorInit {
|
|
16
|
-
/**
|
|
17
|
-
* How often to add a latency check event (ms)
|
|
18
|
-
*/
|
|
19
|
-
latencyCheckIntervalMs?: number
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* How often to summarize latency check events. null or 0 disables event firing
|
|
23
|
-
*/
|
|
24
|
-
dataEmitIntervalMs?: number
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* What cb-style async function to use
|
|
28
|
-
*/
|
|
29
|
-
asyncTestFn?: (cb: () => void) => void
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* What percent (+/-) of latencyCheckIntervalMs should we randomly use? This helps avoid alignment to other events.
|
|
33
|
-
*/
|
|
34
|
-
latencyRandomPercentage?: number
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface SummaryObject {
|
|
38
|
-
/**
|
|
39
|
-
* How many events were called
|
|
40
|
-
*/
|
|
41
|
-
events: number
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* What was the min time for a cb to be called
|
|
45
|
-
*/
|
|
46
|
-
minMs: number
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* What was the max time for a cb to be called
|
|
50
|
-
*/
|
|
51
|
-
maxMs: number
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* What was the average time for a cb to be called
|
|
55
|
-
*/
|
|
56
|
-
avgMs: number
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* How long this interval was in ms
|
|
60
|
-
*/
|
|
61
|
-
lengthMs: number
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
interface LatencyData {
|
|
65
|
-
startTime: number
|
|
66
|
-
events: number
|
|
67
|
-
minMs: number
|
|
68
|
-
maxMs: number
|
|
69
|
-
totalMs: number
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* A class to monitor latency of any async function which works in a browser or node. This works by periodically calling
|
|
74
|
-
* the asyncTestFn and timing how long it takes the callback to be called. It can also periodically emit stats about this.
|
|
75
|
-
* This can be disabled and stats can be pulled via setting dataEmitIntervalMs = 0.
|
|
76
|
-
*
|
|
77
|
-
* @extends {EventEmitter}
|
|
78
|
-
*
|
|
79
|
-
* The default implementation is an event loop latency monitor. This works by firing periodic events into the event loop
|
|
80
|
-
* and timing how long it takes to get back.
|
|
81
|
-
*
|
|
82
|
-
* @example
|
|
83
|
-
* const monitor = new LatencyMonitor();
|
|
84
|
-
* monitor.on('data', (summary) => console.log('Event Loop Latency: %O', summary));
|
|
85
|
-
*
|
|
86
|
-
* @example
|
|
87
|
-
* const monitor = new LatencyMonitor({latencyCheckIntervalMs: 1000, dataEmitIntervalMs: 60000, asyncTestFn:ping});
|
|
88
|
-
* monitor.on('data', (summary) => console.log('Ping Pong Latency: %O', summary));
|
|
89
|
-
*/
|
|
90
|
-
export class LatencyMonitor extends EventEmitter<LatencyMonitorEvents> {
|
|
91
|
-
private readonly latencyCheckIntervalMs: number
|
|
92
|
-
private readonly latencyRandomPercentage: number
|
|
93
|
-
private readonly latencyCheckMultiply: number
|
|
94
|
-
private readonly latencyCheckSubtract: number
|
|
95
|
-
private readonly dataEmitIntervalMs?: number
|
|
96
|
-
private readonly asyncTestFn?: (cb: () => void) => void
|
|
97
|
-
|
|
98
|
-
private readonly now: (num?: any) => any
|
|
99
|
-
private readonly getDeltaMS: (num: number) => number
|
|
100
|
-
private visibilityChangeEmitter?: VisibilityChangeEmitter
|
|
101
|
-
private latencyData: LatencyData
|
|
102
|
-
private checkLatencyID?: NodeJS.Timeout
|
|
103
|
-
private emitIntervalID?: NodeJS.Timeout
|
|
104
|
-
|
|
105
|
-
constructor (init: LatencyMonitorInit = {}) {
|
|
106
|
-
super()
|
|
107
|
-
|
|
108
|
-
const { latencyCheckIntervalMs, dataEmitIntervalMs, asyncTestFn, latencyRandomPercentage } = init
|
|
109
|
-
|
|
110
|
-
// 0 isn't valid here, so its ok to use ||
|
|
111
|
-
this.latencyCheckIntervalMs = latencyCheckIntervalMs ?? 500 // 0.5s
|
|
112
|
-
this.latencyRandomPercentage = latencyRandomPercentage ?? 10
|
|
113
|
-
this.latencyCheckMultiply = 2 * (this.latencyRandomPercentage / 100.0) * this.latencyCheckIntervalMs
|
|
114
|
-
this.latencyCheckSubtract = this.latencyCheckMultiply / 2
|
|
115
|
-
|
|
116
|
-
this.dataEmitIntervalMs = (dataEmitIntervalMs === null || dataEmitIntervalMs === 0)
|
|
117
|
-
? undefined
|
|
118
|
-
: dataEmitIntervalMs ?? 5 * 1000 // 5s
|
|
119
|
-
log('latencyCheckIntervalMs: %s dataEmitIntervalMs: %s',
|
|
120
|
-
this.latencyCheckIntervalMs, this.dataEmitIntervalMs)
|
|
121
|
-
if (this.dataEmitIntervalMs != null) {
|
|
122
|
-
log('Expecting ~%s events per summary', this.latencyCheckIntervalMs / this.dataEmitIntervalMs)
|
|
123
|
-
} else {
|
|
124
|
-
log('Not emitting summaries')
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
this.asyncTestFn = asyncTestFn // If there is no asyncFn, we measure latency
|
|
128
|
-
|
|
129
|
-
// If process: use high resolution timer
|
|
130
|
-
if (globalThis.process?.hrtime != null) {
|
|
131
|
-
log('Using process.hrtime for timing')
|
|
132
|
-
this.now = globalThis.process.hrtime // eslint-disable-line no-undef
|
|
133
|
-
this.getDeltaMS = (startTime) => {
|
|
134
|
-
const hrtime = this.now(startTime)
|
|
135
|
-
return (hrtime[0] * 1000) + (hrtime[1] / 1000000)
|
|
136
|
-
}
|
|
137
|
-
// Let's try for a timer that only monotonically increases
|
|
138
|
-
} else if (typeof window !== 'undefined' && window.performance?.now != null) {
|
|
139
|
-
log('Using performance.now for timing')
|
|
140
|
-
this.now = window.performance.now.bind(window.performance)
|
|
141
|
-
this.getDeltaMS = (startTime) => Math.round(this.now() - startTime)
|
|
142
|
-
} else {
|
|
143
|
-
log('Using Date.now for timing')
|
|
144
|
-
this.now = Date.now
|
|
145
|
-
this.getDeltaMS = (startTime) => this.now() - startTime
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
this.latencyData = this.initLatencyData()
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
start (): void {
|
|
152
|
-
// We check for isBrowser because of browsers set max rates of timeouts when a page is hidden,
|
|
153
|
-
// so we fall back to another library
|
|
154
|
-
// See: http://stackoverflow.com/questions/6032429/chrome-timeouts-interval-suspended-in-background-tabs
|
|
155
|
-
if (isBrowser()) {
|
|
156
|
-
this.visibilityChangeEmitter = new VisibilityChangeEmitter()
|
|
157
|
-
|
|
158
|
-
this.visibilityChangeEmitter.addEventListener('visibilityChange', (evt) => {
|
|
159
|
-
const { detail: pageInFocus } = evt
|
|
160
|
-
|
|
161
|
-
if (pageInFocus) {
|
|
162
|
-
this._startTimers()
|
|
163
|
-
} else {
|
|
164
|
-
this._emitSummary()
|
|
165
|
-
this._stopTimers()
|
|
166
|
-
}
|
|
167
|
-
})
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if (this.visibilityChangeEmitter?.isVisible() === true) {
|
|
171
|
-
this._startTimers()
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
stop (): void {
|
|
176
|
-
this._stopTimers()
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Start internal timers
|
|
181
|
-
*
|
|
182
|
-
* @private
|
|
183
|
-
*/
|
|
184
|
-
_startTimers (): void {
|
|
185
|
-
// Timer already started, ignore this
|
|
186
|
-
if (this.checkLatencyID != null) {
|
|
187
|
-
return
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
this.checkLatency()
|
|
191
|
-
|
|
192
|
-
if (this.dataEmitIntervalMs != null) {
|
|
193
|
-
this.emitIntervalID = setInterval(() => { this._emitSummary() }, this.dataEmitIntervalMs)
|
|
194
|
-
if (typeof this.emitIntervalID.unref === 'function') {
|
|
195
|
-
this.emitIntervalID.unref() // Doesn't block exit
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Stop internal timers
|
|
202
|
-
*
|
|
203
|
-
* @private
|
|
204
|
-
*/
|
|
205
|
-
_stopTimers (): void {
|
|
206
|
-
if (this.checkLatencyID != null) {
|
|
207
|
-
clearTimeout(this.checkLatencyID)
|
|
208
|
-
this.checkLatencyID = undefined
|
|
209
|
-
}
|
|
210
|
-
if (this.emitIntervalID != null) {
|
|
211
|
-
clearInterval(this.emitIntervalID)
|
|
212
|
-
this.emitIntervalID = undefined
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Emit summary only if there were events. It might not have any events if it was forced via a page hidden/show
|
|
218
|
-
*
|
|
219
|
-
* @private
|
|
220
|
-
*/
|
|
221
|
-
_emitSummary (): void {
|
|
222
|
-
const summary = this.getSummary()
|
|
223
|
-
if (summary.events > 0) {
|
|
224
|
-
this.dispatchEvent(new CustomEvent<SummaryObject>('data', {
|
|
225
|
-
detail: summary
|
|
226
|
-
}))
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Calling this function will end the collection period. If a timing event was already fired and somewhere in the queue,
|
|
232
|
-
* it will not count for this time period
|
|
233
|
-
*/
|
|
234
|
-
getSummary (): SummaryObject {
|
|
235
|
-
// We might want to adjust for the number of expected events
|
|
236
|
-
// Example: first 1 event it comes back, then such a long blocker that the next emit check comes
|
|
237
|
-
// Then this fires - looks like no latency!!
|
|
238
|
-
const latency = {
|
|
239
|
-
events: this.latencyData.events,
|
|
240
|
-
minMs: this.latencyData.minMs,
|
|
241
|
-
maxMs: this.latencyData.maxMs,
|
|
242
|
-
avgMs: this.latencyData.events > 0
|
|
243
|
-
? this.latencyData.totalMs / this.latencyData.events
|
|
244
|
-
: Number.POSITIVE_INFINITY,
|
|
245
|
-
lengthMs: this.getDeltaMS(this.latencyData.startTime)
|
|
246
|
-
}
|
|
247
|
-
this.latencyData = this.initLatencyData() // Clear
|
|
248
|
-
|
|
249
|
-
log.trace('Summary: %O', latency)
|
|
250
|
-
return latency
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Randomly calls an async fn every roughly latencyCheckIntervalMs (plus some randomness). If no async fn is found,
|
|
255
|
-
* it will simply report on event loop latency.
|
|
256
|
-
*/
|
|
257
|
-
checkLatency (): void {
|
|
258
|
-
// Randomness is needed to avoid alignment by accident to regular things in the event loop
|
|
259
|
-
const randomness = (Math.random() * this.latencyCheckMultiply) - this.latencyCheckSubtract
|
|
260
|
-
|
|
261
|
-
// We use this to ensure that in case some overlap somehow, we don't take the wrong startTime/offset
|
|
262
|
-
const localData = {
|
|
263
|
-
deltaOffset: Math.ceil(this.latencyCheckIntervalMs + randomness),
|
|
264
|
-
startTime: this.now()
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
const cb = (): void => {
|
|
268
|
-
// We are already stopped, ignore this datapoint
|
|
269
|
-
if (this.checkLatencyID == null) {
|
|
270
|
-
return
|
|
271
|
-
}
|
|
272
|
-
const deltaMS = this.getDeltaMS(localData.startTime) - localData.deltaOffset
|
|
273
|
-
this.checkLatency() // Start again ASAP
|
|
274
|
-
|
|
275
|
-
// Add the data point. If this gets complex, refactor it
|
|
276
|
-
this.latencyData.events++
|
|
277
|
-
this.latencyData.minMs = Math.min(this.latencyData.minMs, deltaMS)
|
|
278
|
-
this.latencyData.maxMs = Math.max(this.latencyData.maxMs, deltaMS)
|
|
279
|
-
this.latencyData.totalMs += deltaMS
|
|
280
|
-
log.trace('MS: %s Data: %O', deltaMS, this.latencyData)
|
|
281
|
-
}
|
|
282
|
-
log.trace('localData: %O', localData)
|
|
283
|
-
|
|
284
|
-
this.checkLatencyID = setTimeout(() => {
|
|
285
|
-
// This gets rid of including event loop
|
|
286
|
-
if (this.asyncTestFn != null) {
|
|
287
|
-
// Clear timing related things
|
|
288
|
-
localData.deltaOffset = 0
|
|
289
|
-
localData.startTime = this.now()
|
|
290
|
-
this.asyncTestFn(cb)
|
|
291
|
-
} else {
|
|
292
|
-
// setTimeout is not more accurate than 1ms, so this will ensure positive numbers. Add 1 to emitted data to remove.
|
|
293
|
-
// This is not the best, but for now it'll be just fine. This isn't meant to be sub ms accurate.
|
|
294
|
-
localData.deltaOffset -= 1
|
|
295
|
-
// If there is no function to test, we mean check latency which is a special case that is really cb => cb()
|
|
296
|
-
// We avoid that for the few extra function all overheads. Also, we want to keep the timers different
|
|
297
|
-
cb()
|
|
298
|
-
}
|
|
299
|
-
}, localData.deltaOffset)
|
|
300
|
-
|
|
301
|
-
if (typeof this.checkLatencyID.unref === 'function') {
|
|
302
|
-
this.checkLatencyID.unref() // Doesn't block exit
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
initLatencyData (): LatencyData {
|
|
307
|
-
return {
|
|
308
|
-
startTime: this.now(),
|
|
309
|
-
minMs: Number.POSITIVE_INFINITY,
|
|
310
|
-
maxMs: Number.NEGATIVE_INFINITY,
|
|
311
|
-
events: 0,
|
|
312
|
-
totalMs: 0
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
function isBrowser (): boolean {
|
|
318
|
-
return typeof globalThis.window !== 'undefined'
|
|
319
|
-
}
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This code is based on `latency-monitor` (https://github.com/mlucool/latency-monitor) by `mlucool` (https://github.com/mlucool), available under Apache License 2.0 (https://github.com/mlucool/latency-monitor/blob/master/LICENSE)
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
6
|
-
import { logger } from '@libp2p/logger'
|
|
7
|
-
|
|
8
|
-
const log = logger('libp2p:connection-manager:latency-monitor:visibility-change-emitter')
|
|
9
|
-
|
|
10
|
-
interface VisibilityChangeEmitterEvents {
|
|
11
|
-
'visibilityChange': CustomEvent<boolean>
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
type Hidden = 'hidden' | 'mozHidden' | 'msHidden' | 'webkitHidden'
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Listen to page visibility change events (i.e. when the page is focused / blurred) by an event emitter.
|
|
18
|
-
*
|
|
19
|
-
* Warning: This does not work on all browsers, but should work on all modern browsers
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
*
|
|
23
|
-
* const myVisibilityEmitter = new VisibilityChangeEmitter();
|
|
24
|
-
*
|
|
25
|
-
* myVisibilityEmitter.on('visibilityChange', (pageInFocus) => {
|
|
26
|
-
* if ( pageInFocus ){
|
|
27
|
-
* // Page is in focus
|
|
28
|
-
* console.log('In focus');
|
|
29
|
-
* }
|
|
30
|
-
* else {
|
|
31
|
-
* // Page is blurred
|
|
32
|
-
* console.log('Out of focus');
|
|
33
|
-
* }
|
|
34
|
-
* });
|
|
35
|
-
* // To access the visibility state directly, call:
|
|
36
|
-
* console.log('Am I focused now? ' + myVisibilityEmitter.isVisible());
|
|
37
|
-
*/
|
|
38
|
-
export class VisibilityChangeEmitter extends EventEmitter<VisibilityChangeEmitterEvents> {
|
|
39
|
-
private hidden: Hidden
|
|
40
|
-
private visibilityChange: string
|
|
41
|
-
|
|
42
|
-
constructor () {
|
|
43
|
-
super()
|
|
44
|
-
|
|
45
|
-
this.hidden = 'hidden'
|
|
46
|
-
this.visibilityChange = 'visibilityChange'
|
|
47
|
-
|
|
48
|
-
if (globalThis.document != null) {
|
|
49
|
-
this._initializeVisibilityVarNames()
|
|
50
|
-
this._addVisibilityChangeListener()
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* document.hidden and document.visibilityChange are the two variables we need to check for;
|
|
56
|
-
* Since these variables are named differently in different browsers, this function sets
|
|
57
|
-
* the appropriate name based on the browser being used. Once executed, tha actual names of
|
|
58
|
-
* document.hidden and document.visibilityChange are found in this._hidden and this._visibilityChange
|
|
59
|
-
* respectively
|
|
60
|
-
*
|
|
61
|
-
* @private
|
|
62
|
-
*/
|
|
63
|
-
_initializeVisibilityVarNames (): void {
|
|
64
|
-
let hidden: Hidden = 'hidden'
|
|
65
|
-
let visibilityChange = 'visibilitychange'
|
|
66
|
-
|
|
67
|
-
if (typeof globalThis.document.hidden !== 'undefined') { // Opera 12.10 and Firefox 18 and later support
|
|
68
|
-
hidden = 'hidden'
|
|
69
|
-
visibilityChange = 'visibilitychange'
|
|
70
|
-
// @ts-expect-error mozHidden is a non-standard field name
|
|
71
|
-
} else if (typeof globalThis.document.mozHidden !== 'undefined') {
|
|
72
|
-
hidden = 'mozHidden'
|
|
73
|
-
visibilityChange = 'mozvisibilitychange'
|
|
74
|
-
// @ts-expect-error msHidden is a non-standard field name
|
|
75
|
-
} else if (typeof globalThis.document.msHidden !== 'undefined') {
|
|
76
|
-
hidden = 'msHidden'
|
|
77
|
-
visibilityChange = 'msvisibilitychange'
|
|
78
|
-
// @ts-expect-error webkitHidden is a non-standard field name
|
|
79
|
-
} else if (typeof globalThis.document.webkitHidden !== 'undefined') {
|
|
80
|
-
hidden = 'webkitHidden'
|
|
81
|
-
visibilityChange = 'webkitvisibilitychange'
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
this.hidden = hidden
|
|
85
|
-
this.visibilityChange = visibilityChange
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Adds an event listener on the document that listens to changes in document.visibilityChange
|
|
90
|
-
* (or whatever name by which the visibilityChange variable is known in the browser)
|
|
91
|
-
*
|
|
92
|
-
* @private
|
|
93
|
-
*/
|
|
94
|
-
_addVisibilityChangeListener (): void {
|
|
95
|
-
// @ts-expect-error cannot index document object with string key
|
|
96
|
-
if (typeof globalThis.document.addEventListener === 'undefined' || typeof document[this.hidden] === 'undefined') {
|
|
97
|
-
log('Checking page visibility requires a browser that supports the Page Visibility API.')
|
|
98
|
-
} else {
|
|
99
|
-
// Handle page visibility change
|
|
100
|
-
globalThis.document.addEventListener(this.visibilityChange, this._handleVisibilityChange.bind(this), false)
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* The function returns ```true``` if the page is visible or ```false``` if the page is not visible and
|
|
106
|
-
* ```undefined``` if the page visibility API is not supported by the browser.
|
|
107
|
-
*/
|
|
108
|
-
isVisible (): boolean | undefined {
|
|
109
|
-
// @ts-expect-error cannot index document object with string key
|
|
110
|
-
if (this.hidden === undefined || document[this.hidden] === undefined) {
|
|
111
|
-
return undefined
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// @ts-expect-error cannot index document object with string key
|
|
115
|
-
return document[this.hidden] == null
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* The function that is called when document.visibilityChange has changed
|
|
120
|
-
* It emits an event called visibilityChange and sends the value of document.hidden as a
|
|
121
|
-
* parameter
|
|
122
|
-
*
|
|
123
|
-
* @private
|
|
124
|
-
*/
|
|
125
|
-
_handleVisibilityChange (): void {
|
|
126
|
-
// @ts-expect-error cannot index document object with string key
|
|
127
|
-
const visible = globalThis.document[this.hidden] === false
|
|
128
|
-
log(visible ? 'Page Visible' : 'Page Hidden')
|
|
129
|
-
|
|
130
|
-
// Emit the event
|
|
131
|
-
this.dispatchEvent(new CustomEvent<boolean>('visibilityChange', {
|
|
132
|
-
detail: visible
|
|
133
|
-
}))
|
|
134
|
-
}
|
|
135
|
-
}
|
package/src/constants.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* How long in ms a dial attempt is allowed to take
|
|
4
|
-
*/
|
|
5
|
-
export const DIAL_TIMEOUT = 30e3
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* How long in ms an inbound connection upgrade is allowed to take
|
|
9
|
-
*/
|
|
10
|
-
export const INBOUND_UPGRADE_TIMEOUT = 30e3
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Maximum allowed concurrent dials
|
|
14
|
-
*/
|
|
15
|
-
export const MAX_PARALLEL_DIALS = 100
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Allowed parallel dials per DialRequest
|
|
19
|
-
*/
|
|
20
|
-
export const MAX_PER_PEER_DIALS = 4
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Maximum number of allowed addresses to attempt to dial
|
|
24
|
-
*/
|
|
25
|
-
export const MAX_ADDRS_TO_DIAL = 25
|
|
26
|
-
|
|
27
|
-
export const METRICS = {
|
|
28
|
-
computeThrottleMaxQueueSize: 1000,
|
|
29
|
-
computeThrottleTimeout: 2000,
|
|
30
|
-
movingAverageIntervals: [
|
|
31
|
-
60 * 1000, // 1 minute
|
|
32
|
-
5 * 60 * 1000, // 5 minutes
|
|
33
|
-
15 * 60 * 1000 // 15 minutes
|
|
34
|
-
],
|
|
35
|
-
maxOldPeersRetention: 50
|
|
36
|
-
}
|