@libp2p/interface-compliance-tests 6.1.8-02f285fc8 → 6.1.8-2feaeddb4
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/src/mocks/index.d.ts +0 -2
- package/dist/src/mocks/index.d.ts.map +1 -1
- package/dist/src/mocks/index.js +0 -2
- package/dist/src/mocks/index.js.map +1 -1
- package/dist/src/transport/index.d.ts.map +1 -1
- package/dist/src/transport/index.js +204 -3
- package/dist/src/transport/index.js.map +1 -1
- package/package.json +14 -15
- package/src/mocks/index.ts +0 -2
- package/src/transport/index.ts +261 -5
- package/dist/src/mocks/connection-gater.d.ts +0 -3
- package/dist/src/mocks/connection-gater.d.ts.map +0 -1
- package/dist/src/mocks/connection-gater.js +0 -17
- package/dist/src/mocks/connection-gater.js.map +0 -1
- package/dist/src/mocks/metrics.d.ts +0 -3
- package/dist/src/mocks/metrics.d.ts.map +0 -1
- package/dist/src/mocks/metrics.js +0 -286
- package/dist/src/mocks/metrics.js.map +0 -1
- package/dist/src/mocks/peer-discovery.d.ts +0 -21
- package/dist/src/mocks/peer-discovery.d.ts.map +0 -1
- package/dist/src/mocks/peer-discovery.js +0 -47
- package/dist/src/mocks/peer-discovery.js.map +0 -1
- package/src/mocks/connection-gater.ts +0 -18
- package/src/mocks/metrics.ts +0 -385
- package/src/mocks/peer-discovery.ts +0 -59
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { generateKeyPair } from '@libp2p/crypto/keys';
|
|
2
|
-
import { TypedEventEmitter, peerDiscoverySymbol } from '@libp2p/interface';
|
|
3
|
-
import { peerIdFromPrivateKey } from '@libp2p/peer-id';
|
|
4
|
-
import { multiaddr } from '@multiformats/multiaddr';
|
|
5
|
-
/**
|
|
6
|
-
* Emits 'peer' events on discovery.
|
|
7
|
-
*/
|
|
8
|
-
export class MockDiscovery extends TypedEventEmitter {
|
|
9
|
-
options;
|
|
10
|
-
_isRunning;
|
|
11
|
-
_timer;
|
|
12
|
-
constructor(init = {}) {
|
|
13
|
-
super();
|
|
14
|
-
this.options = init;
|
|
15
|
-
this._isRunning = false;
|
|
16
|
-
}
|
|
17
|
-
[peerDiscoverySymbol] = this;
|
|
18
|
-
start() {
|
|
19
|
-
this._isRunning = true;
|
|
20
|
-
this._discoverPeer();
|
|
21
|
-
}
|
|
22
|
-
stop() {
|
|
23
|
-
clearTimeout(this._timer);
|
|
24
|
-
this._isRunning = false;
|
|
25
|
-
}
|
|
26
|
-
isStarted() {
|
|
27
|
-
return this._isRunning;
|
|
28
|
-
}
|
|
29
|
-
_discoverPeer() {
|
|
30
|
-
if (!this._isRunning)
|
|
31
|
-
return;
|
|
32
|
-
generateKeyPair('Ed25519')
|
|
33
|
-
.then(key => {
|
|
34
|
-
const peerId = peerIdFromPrivateKey(key);
|
|
35
|
-
this._timer = setTimeout(() => {
|
|
36
|
-
this.safeDispatchEvent('peer', {
|
|
37
|
-
detail: {
|
|
38
|
-
id: peerId,
|
|
39
|
-
multiaddrs: [multiaddr('/ip4/127.0.0.1/tcp/8000')]
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
}, this.options.discoveryDelay ?? 1000);
|
|
43
|
-
})
|
|
44
|
-
.catch(() => { });
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
//# sourceMappingURL=peer-discovery.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"peer-discovery.js","sourceRoot":"","sources":["../../../src/mocks/peer-discovery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAC1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAA;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAOnD;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,iBAAsC;IACvD,OAAO,CAAmB;IAClC,UAAU,CAAS;IACnB,MAAM,CAAK;IAEnB,YAAa,IAAI,GAAG,EAAE;QACpB,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA;QACnB,IAAI,CAAC,UAAU,GAAG,KAAK,CAAA;IACzB,CAAC;IAEQ,CAAC,mBAAmB,CAAC,GAAG,IAAI,CAAA;IAErC,KAAK;QACH,IAAI,CAAC,UAAU,GAAG,IAAI,CAAA;QACtB,IAAI,CAAC,aAAa,EAAE,CAAA;IACtB,CAAC;IAED,IAAI;QACF,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QACzB,IAAI,CAAC,UAAU,GAAG,KAAK,CAAA;IACzB,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,UAAU,CAAA;IACxB,CAAC;IAED,aAAa;QACX,IAAI,CAAC,IAAI,CAAC,UAAU;YAAE,OAAM;QAE5B,eAAe,CAAC,SAAS,CAAC;aACvB,IAAI,CAAC,GAAG,CAAC,EAAE;YACV,MAAM,MAAM,GAAG,oBAAoB,CAAC,GAAG,CAAC,CAAA;YACxC,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC,GAAG,EAAE;gBAC5B,IAAI,CAAC,iBAAiB,CAAW,MAAM,EAAE;oBACvC,MAAM,EAAE;wBACN,EAAE,EAAE,MAAM;wBACV,UAAU,EAAE,CAAC,SAAS,CAAC,yBAAyB,CAAC,CAAC;qBACnD;iBACF,CAAC,CAAA;YACJ,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,cAAc,IAAI,IAAI,CAAC,CAAA;QACzC,CAAC,CAAC;aACD,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;IACpB,CAAC;CACF"}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { ConnectionGater } from '@libp2p/interface'
|
|
2
|
-
|
|
3
|
-
export function mockConnectionGater (): ConnectionGater {
|
|
4
|
-
return {
|
|
5
|
-
denyDialPeer: async () => Promise.resolve(false),
|
|
6
|
-
denyDialMultiaddr: async () => Promise.resolve(false),
|
|
7
|
-
denyInboundConnection: async () => Promise.resolve(false),
|
|
8
|
-
denyOutboundConnection: async () => Promise.resolve(false),
|
|
9
|
-
denyInboundEncryptedConnection: async () => Promise.resolve(false),
|
|
10
|
-
denyOutboundEncryptedConnection: async () => Promise.resolve(false),
|
|
11
|
-
denyInboundUpgradedConnection: async () => Promise.resolve(false),
|
|
12
|
-
denyOutboundUpgradedConnection: async () => Promise.resolve(false),
|
|
13
|
-
denyInboundRelayReservation: async () => Promise.resolve(false),
|
|
14
|
-
denyOutboundRelayedConnection: async () => Promise.resolve(false),
|
|
15
|
-
denyInboundRelayedConnection: async () => Promise.resolve(false),
|
|
16
|
-
filterMultiaddrForPeer: async () => Promise.resolve(true)
|
|
17
|
-
}
|
|
18
|
-
}
|
package/src/mocks/metrics.ts
DELETED
|
@@ -1,385 +0,0 @@
|
|
|
1
|
-
import { TDigest } from 'tdigest'
|
|
2
|
-
import type { MultiaddrConnection, Stream, Connection, Metric, MetricGroup, StopTimer, Metrics, CalculatedMetricOptions, MetricOptions, Histogram, HistogramOptions, HistogramGroup, Summary, SummaryOptions, SummaryGroup, CalculatedHistogramOptions, CalculatedSummaryOptions } from '@libp2p/interface'
|
|
3
|
-
|
|
4
|
-
class DefaultMetric implements Metric {
|
|
5
|
-
public value: number = 0
|
|
6
|
-
|
|
7
|
-
update (value: number): void {
|
|
8
|
-
this.value = value
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
increment (value: number = 1): void {
|
|
12
|
-
this.value += value
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
decrement (value: number = 1): void {
|
|
16
|
-
this.value -= value
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
reset (): void {
|
|
20
|
-
this.value = 0
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
timer (): StopTimer {
|
|
24
|
-
const start = Date.now()
|
|
25
|
-
|
|
26
|
-
return () => {
|
|
27
|
-
this.value = Date.now() - start
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
class DefaultGroupMetric implements MetricGroup {
|
|
33
|
-
public values: Record<string, number> = {}
|
|
34
|
-
|
|
35
|
-
update (values: Record<string, number>): void {
|
|
36
|
-
Object.entries(values).forEach(([key, value]) => {
|
|
37
|
-
this.values[key] = value
|
|
38
|
-
})
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
increment (values: Record<string, number | unknown>): void {
|
|
42
|
-
Object.entries(values).forEach(([key, value]) => {
|
|
43
|
-
this.values[key] = this.values[key] ?? 0
|
|
44
|
-
const inc = typeof value === 'number' ? value : 1
|
|
45
|
-
|
|
46
|
-
this.values[key] += Number(inc)
|
|
47
|
-
})
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
decrement (values: Record<string, number | unknown>): void {
|
|
51
|
-
Object.entries(values).forEach(([key, value]) => {
|
|
52
|
-
this.values[key] = this.values[key] ?? 0
|
|
53
|
-
const dec = typeof value === 'number' ? value : 1
|
|
54
|
-
|
|
55
|
-
this.values[key] -= Number(dec)
|
|
56
|
-
})
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
reset (): void {
|
|
60
|
-
this.values = {}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
timer (key: string): StopTimer {
|
|
64
|
-
const start = Date.now()
|
|
65
|
-
|
|
66
|
-
return () => {
|
|
67
|
-
this.values[key] = Date.now() - start
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
class DefaultHistogram implements Histogram {
|
|
73
|
-
public bucketValues = new Map<number, number>()
|
|
74
|
-
public countValue: number = 0
|
|
75
|
-
public sumValue: number = 0
|
|
76
|
-
|
|
77
|
-
constructor (opts: HistogramOptions) {
|
|
78
|
-
const buckets = [
|
|
79
|
-
...(opts.buckets ?? [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]),
|
|
80
|
-
Infinity
|
|
81
|
-
]
|
|
82
|
-
for (const bucket of buckets) {
|
|
83
|
-
this.bucketValues.set(bucket, 0)
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
observe (value: number): void {
|
|
88
|
-
this.countValue++
|
|
89
|
-
this.sumValue += value
|
|
90
|
-
|
|
91
|
-
for (const [bucket, count] of this.bucketValues.entries()) {
|
|
92
|
-
if (value <= bucket) {
|
|
93
|
-
this.bucketValues.set(bucket, count + 1)
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
reset (): void {
|
|
99
|
-
this.countValue = 0
|
|
100
|
-
this.sumValue = 0
|
|
101
|
-
for (const bucket of this.bucketValues.keys()) {
|
|
102
|
-
this.bucketValues.set(bucket, 0)
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
timer (): StopTimer {
|
|
107
|
-
const start = Date.now()
|
|
108
|
-
|
|
109
|
-
return () => {
|
|
110
|
-
this.observe(Date.now() - start)
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
class DefaultHistogramGroup implements HistogramGroup {
|
|
116
|
-
public histograms: Record<string, DefaultHistogram> = {}
|
|
117
|
-
|
|
118
|
-
constructor (opts: HistogramOptions) {
|
|
119
|
-
this.histograms = {}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
observe (values: Partial<Record<string, number>>): void {
|
|
123
|
-
for (const [key, value] of Object.entries(values) as Array<[string, number]>) {
|
|
124
|
-
if (this.histograms[key] === undefined) {
|
|
125
|
-
this.histograms[key] = new DefaultHistogram({})
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
this.histograms[key].observe(value)
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
reset (): void {
|
|
133
|
-
for (const histogram of Object.values(this.histograms)) {
|
|
134
|
-
histogram.reset()
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
timer (key: string): StopTimer {
|
|
139
|
-
const start = Date.now()
|
|
140
|
-
|
|
141
|
-
return () => {
|
|
142
|
-
this.observe({ [key]: Date.now() - start })
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
class DefaultSummary implements Summary {
|
|
148
|
-
public sumValue: number = 0
|
|
149
|
-
public countValue: number = 0
|
|
150
|
-
public percentiles: number[]
|
|
151
|
-
public tdigest = new TDigest(0.01)
|
|
152
|
-
private readonly compressCount: number
|
|
153
|
-
|
|
154
|
-
constructor (opts: SummaryOptions) {
|
|
155
|
-
this.percentiles = opts.percentiles ?? [0.01, 0.05, 0.5, 0.9, 0.95, 0.99, 0.999]
|
|
156
|
-
this.compressCount = opts.compressCount ?? 1000
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
observe (value: number): void {
|
|
160
|
-
this.sumValue += value
|
|
161
|
-
this.countValue++
|
|
162
|
-
|
|
163
|
-
this.tdigest.push(value)
|
|
164
|
-
if (this.tdigest.size() > this.compressCount) {
|
|
165
|
-
this.tdigest.compress()
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
reset (): void {
|
|
170
|
-
this.sumValue = 0
|
|
171
|
-
this.countValue = 0
|
|
172
|
-
|
|
173
|
-
this.tdigest.reset()
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
timer (): StopTimer {
|
|
177
|
-
const start = Date.now()
|
|
178
|
-
|
|
179
|
-
return () => {
|
|
180
|
-
this.observe(Date.now() - start)
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
class DefaultSummaryGroup implements SummaryGroup {
|
|
186
|
-
public summaries: Record<string, DefaultSummary> = {}
|
|
187
|
-
private readonly opts: SummaryOptions
|
|
188
|
-
|
|
189
|
-
constructor (opts: SummaryOptions) {
|
|
190
|
-
this.summaries = {}
|
|
191
|
-
this.opts = opts
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
observe (values: Record<string, number>): void {
|
|
195
|
-
for (const [key, value] of Object.entries(values)) {
|
|
196
|
-
if (this.summaries[key] === undefined) {
|
|
197
|
-
this.summaries[key] = new DefaultSummary(this.opts)
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
this.summaries[key].observe(value)
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
reset (): void {
|
|
205
|
-
for (const summary of Object.values(this.summaries)) {
|
|
206
|
-
summary.reset()
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
timer (key: string): StopTimer {
|
|
211
|
-
const start = Date.now()
|
|
212
|
-
|
|
213
|
-
return () => {
|
|
214
|
-
this.observe({ [key]: Date.now() - start })
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
class MockMetrics implements Metrics {
|
|
220
|
-
public metrics = new Map<string, any>()
|
|
221
|
-
|
|
222
|
-
trackMultiaddrConnection (maConn: MultiaddrConnection): void {
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
trackProtocolStream (stream: Stream, connection: Connection): void {
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
registerMetric (name: string, opts: CalculatedMetricOptions): void
|
|
231
|
-
registerMetric (name: string, opts?: MetricOptions): Metric
|
|
232
|
-
registerMetric (name: string, opts: any): any {
|
|
233
|
-
if (name == null || name.trim() === '') {
|
|
234
|
-
throw new Error('Metric name is required')
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
if (opts?.calculate != null) {
|
|
238
|
-
// calculated metric
|
|
239
|
-
this.metrics.set(name, opts.calculate)
|
|
240
|
-
return
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
const metric = new DefaultMetric()
|
|
244
|
-
this.metrics.set(name, metric)
|
|
245
|
-
|
|
246
|
-
return metric
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
registerCounter (name: string, opts: CalculatedMetricOptions): void
|
|
250
|
-
registerCounter (name: string, opts?: MetricOptions): Metric
|
|
251
|
-
registerCounter (name: string, opts: any): any {
|
|
252
|
-
if (name == null || name.trim() === '') {
|
|
253
|
-
throw new Error('Metric name is required')
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
if (opts?.calculate != null) {
|
|
257
|
-
// calculated metric
|
|
258
|
-
this.metrics.set(name, opts.calculate)
|
|
259
|
-
return
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
const metric = new DefaultMetric()
|
|
263
|
-
this.metrics.set(name, metric)
|
|
264
|
-
|
|
265
|
-
return metric
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
registerMetricGroup (name: string, opts: CalculatedMetricOptions<Record<string, number>>): void
|
|
269
|
-
registerMetricGroup (name: string, opts?: MetricOptions): MetricGroup
|
|
270
|
-
registerMetricGroup (name: string, opts: any): any {
|
|
271
|
-
if (name == null || name.trim() === '') {
|
|
272
|
-
throw new Error('Metric name is required')
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
if (opts?.calculate != null) {
|
|
276
|
-
// calculated metric
|
|
277
|
-
this.metrics.set(name, opts.calculate)
|
|
278
|
-
return
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const metric = new DefaultGroupMetric()
|
|
282
|
-
this.metrics.set(name, metric)
|
|
283
|
-
|
|
284
|
-
return metric
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
registerCounterGroup (name: string, opts: CalculatedMetricOptions<Record<string, number>>): void
|
|
288
|
-
registerCounterGroup (name: string, opts?: MetricOptions): MetricGroup
|
|
289
|
-
registerCounterGroup (name: string, opts: any): any {
|
|
290
|
-
if (name == null || name.trim() === '') {
|
|
291
|
-
throw new Error('Metric name is required')
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
if (opts?.calculate != null) {
|
|
295
|
-
// calculated metric
|
|
296
|
-
this.metrics.set(name, opts.calculate)
|
|
297
|
-
return
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
const metric = new DefaultGroupMetric()
|
|
301
|
-
this.metrics.set(name, metric)
|
|
302
|
-
|
|
303
|
-
return metric
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
registerHistogram (name: string, opts: CalculatedHistogramOptions): void
|
|
307
|
-
registerHistogram (name: string, opts?: HistogramOptions): Histogram
|
|
308
|
-
registerHistogram (name: string, opts: any = {}): any {
|
|
309
|
-
if (name == null || name.trim() === '') {
|
|
310
|
-
throw new Error('Metric name is required')
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
if (opts?.calculate != null) {
|
|
314
|
-
// calculated metric
|
|
315
|
-
this.metrics.set(name, opts.calculate)
|
|
316
|
-
return
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
const metric = new DefaultHistogram(opts)
|
|
320
|
-
this.metrics.set(name, metric)
|
|
321
|
-
|
|
322
|
-
return metric
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
registerHistogramGroup (name: string, opts: CalculatedHistogramOptions<Record<string, number>>): void
|
|
326
|
-
registerHistogramGroup (name: string, opts?: HistogramOptions): HistogramGroup
|
|
327
|
-
registerHistogramGroup (name: string, opts: any = {}): any {
|
|
328
|
-
if (name == null || name.trim() === '') {
|
|
329
|
-
throw new Error('Metric name is required')
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
if (opts?.calculate != null) {
|
|
333
|
-
// calculated metric
|
|
334
|
-
this.metrics.set(name, opts.calculate)
|
|
335
|
-
return
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
const metric = new DefaultHistogramGroup(opts)
|
|
339
|
-
this.metrics.set(name, metric)
|
|
340
|
-
|
|
341
|
-
return metric
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
registerSummary (name: string, opts: CalculatedSummaryOptions): void
|
|
345
|
-
registerSummary (name: string, opts?: SummaryOptions): Summary
|
|
346
|
-
registerSummary (name: string, opts: any = {}): any {
|
|
347
|
-
if (name == null || name.trim() === '') {
|
|
348
|
-
throw new Error('Metric name is required')
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
if (opts?.calculate != null) {
|
|
352
|
-
// calculated metric
|
|
353
|
-
this.metrics.set(name, opts.calculate)
|
|
354
|
-
return
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const metric = new DefaultSummary(opts)
|
|
358
|
-
this.metrics.set(name, metric)
|
|
359
|
-
|
|
360
|
-
return metric
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
registerSummaryGroup (name: string, opts: CalculatedSummaryOptions<Record<string, number>>): void
|
|
364
|
-
registerSummaryGroup (name: string, opts?: SummaryOptions): SummaryGroup
|
|
365
|
-
registerSummaryGroup (name: string, opts: any = {}): any {
|
|
366
|
-
if (name == null || name.trim() === '') {
|
|
367
|
-
throw new Error('Metric name is required')
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
if (opts?.calculate != null) {
|
|
371
|
-
// calculated metric
|
|
372
|
-
this.metrics.set(name, opts.calculate)
|
|
373
|
-
return
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
const metric = new DefaultSummaryGroup(opts)
|
|
377
|
-
this.metrics.set(name, metric)
|
|
378
|
-
|
|
379
|
-
return metric
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
export function mockMetrics (): () => Metrics {
|
|
384
|
-
return () => new MockMetrics()
|
|
385
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { generateKeyPair } from '@libp2p/crypto/keys'
|
|
2
|
-
import { TypedEventEmitter, peerDiscoverySymbol } from '@libp2p/interface'
|
|
3
|
-
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
|
|
4
|
-
import { multiaddr } from '@multiformats/multiaddr'
|
|
5
|
-
import type { PeerDiscovery, PeerDiscoveryEvents, PeerInfo } from '@libp2p/interface'
|
|
6
|
-
|
|
7
|
-
interface MockDiscoveryInit {
|
|
8
|
-
discoveryDelay?: number
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Emits 'peer' events on discovery.
|
|
13
|
-
*/
|
|
14
|
-
export class MockDiscovery extends TypedEventEmitter<PeerDiscoveryEvents> implements PeerDiscovery {
|
|
15
|
-
public readonly options: MockDiscoveryInit
|
|
16
|
-
private _isRunning: boolean
|
|
17
|
-
private _timer: any
|
|
18
|
-
|
|
19
|
-
constructor (init = {}) {
|
|
20
|
-
super()
|
|
21
|
-
|
|
22
|
-
this.options = init
|
|
23
|
-
this._isRunning = false
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
readonly [peerDiscoverySymbol] = this
|
|
27
|
-
|
|
28
|
-
start (): void {
|
|
29
|
-
this._isRunning = true
|
|
30
|
-
this._discoverPeer()
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
stop (): void {
|
|
34
|
-
clearTimeout(this._timer)
|
|
35
|
-
this._isRunning = false
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
isStarted (): boolean {
|
|
39
|
-
return this._isRunning
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
_discoverPeer (): void {
|
|
43
|
-
if (!this._isRunning) return
|
|
44
|
-
|
|
45
|
-
generateKeyPair('Ed25519')
|
|
46
|
-
.then(key => {
|
|
47
|
-
const peerId = peerIdFromPrivateKey(key)
|
|
48
|
-
this._timer = setTimeout(() => {
|
|
49
|
-
this.safeDispatchEvent<PeerInfo>('peer', {
|
|
50
|
-
detail: {
|
|
51
|
-
id: peerId,
|
|
52
|
-
multiaddrs: [multiaddr('/ip4/127.0.0.1/tcp/8000')]
|
|
53
|
-
}
|
|
54
|
-
})
|
|
55
|
-
}, this.options.discoveryDelay ?? 1000)
|
|
56
|
-
})
|
|
57
|
-
.catch(() => {})
|
|
58
|
-
}
|
|
59
|
-
}
|