@libp2p/interface 2.0.1 → 2.1.0-7f7ec82ae

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/src/events.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import { setMaxListeners as nodeSetMaxListeners } from 'events'
2
2
 
3
- // create a setMaxListeners that doesn't break browser usage
3
+ /**
4
+ * Create a setMaxListeners that doesn't break browser usage
5
+ */
4
6
  export const setMaxListeners: typeof nodeSetMaxListeners = (n, ...eventTargets) => {
5
7
  try {
6
8
  nodeSetMaxListeners(n, ...eventTargets)
package/src/keys/index.ts CHANGED
@@ -4,15 +4,14 @@ import type { Uint8ArrayList } from 'uint8arraylist'
4
4
 
5
5
  export type KeyType = 'RSA' | 'Ed25519' | 'secp256k1'
6
6
 
7
- interface PublicKeyBase<KeyType extends string, DigestCode extends number = number> {
7
+ export interface RSAPublicKey {
8
8
  /**
9
9
  * The type of this key
10
10
  */
11
- readonly type: KeyType
11
+ readonly type: 'RSA'
12
12
 
13
13
  /**
14
- * The raw public key bytes (for Ed25519 and secp256k1 keys) or PKIX in ASN1
15
- * DER format (for RSA keys)
14
+ * PKIX in ASN1 DER format
16
15
  */
17
16
  readonly raw: Uint8Array
18
17
 
@@ -24,20 +23,101 @@ interface PublicKeyBase<KeyType extends string, DigestCode extends number = numb
24
23
  /**
25
24
  * Returns this public key as a Multihash digest.
26
25
  *
27
- * It contains either an identity hash containing the protobuf version of the
28
- * public key (for Ed25519 and secp256k1 keys) or a sha256 hash of the
29
- * protobuf version of the public key (RSA keys).
26
+ * It contains a sha256 hash of the protobuf version of the public key.
30
27
  */
31
- toMultihash(): MultihashDigest<DigestCode>
28
+ toMultihash(): MultihashDigest<0x12>
32
29
 
33
30
  /**
34
31
  * Return this public key as a CID encoded with the `libp2p-key` codec
35
32
  *
36
- * The digest contains either an identity hash containing the protobuf version
37
- * of the public key (for Ed25519 and secp256k1 keys) or a sha256 hash of the
38
- * protobuf version of the public key (RSA keys).
33
+ * The digest contains a sha256 hash of the protobuf version of the public
34
+ * key.
39
35
  */
40
- toCID(): CID<unknown, 0x72, DigestCode, 1>
36
+ toCID(): CID<unknown, 0x72, 0x12, 1>
37
+
38
+ /**
39
+ * Verify the passed data was signed by the private key corresponding to this
40
+ * public key
41
+ */
42
+ verify(data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean | Promise<boolean>
43
+
44
+ /**
45
+ * Returns this key as a multihash with base58btc encoding
46
+ */
47
+ toString(): string
48
+ }
49
+
50
+ export interface Ed25519PublicKey {
51
+ /**
52
+ * The type of this key
53
+ */
54
+ readonly type: 'Ed25519'
55
+
56
+ /**
57
+ * The raw public key bytes
58
+ */
59
+ readonly raw: Uint8Array
60
+
61
+ /**
62
+ * Returns `true` if the passed object matches this key
63
+ */
64
+ equals(key?: any): boolean
65
+
66
+ /**
67
+ * Returns this public key as an identity hash containing the protobuf wrapped
68
+ * public key
69
+ */
70
+ toMultihash(): MultihashDigest<0x0>
71
+
72
+ /**
73
+ * Return this public key as a CID encoded with the `libp2p-key` codec
74
+ *
75
+ * The digest contains an identity hash containing the protobuf wrapped
76
+ * version of the public key.
77
+ */
78
+ toCID(): CID<unknown, 0x72, 0x0, 1>
79
+
80
+ /**
81
+ * Verify the passed data was signed by the private key corresponding to this
82
+ * public key
83
+ */
84
+ verify(data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean | Promise<boolean>
85
+
86
+ /**
87
+ * Returns this key as a multihash with base58btc encoding
88
+ */
89
+ toString(): string
90
+ }
91
+
92
+ export interface Secp256k1PublicKey {
93
+ /**
94
+ * The type of this key
95
+ */
96
+ readonly type: 'secp256k1'
97
+
98
+ /**
99
+ * The raw public key bytes
100
+ */
101
+ readonly raw: Uint8Array
102
+
103
+ /**
104
+ * Returns `true` if the passed object matches this key
105
+ */
106
+ equals(key?: any): boolean
107
+
108
+ /**
109
+ * Returns this public key as an identity hash containing the protobuf wrapped
110
+ * public key
111
+ */
112
+ toMultihash(): MultihashDigest<0x0>
113
+
114
+ /**
115
+ * Return this public key as a CID encoded with the `libp2p-key` codec
116
+ *
117
+ * The digest contains an identity hash containing the protobuf wrapped
118
+ * version of the public key.
119
+ */
120
+ toCID(): CID<unknown, 0x72, 0x0, 1>
41
121
 
42
122
  /**
43
123
  * Verify the passed data was signed by the private key corresponding to this
@@ -51,9 +131,6 @@ interface PublicKeyBase<KeyType extends string, DigestCode extends number = numb
51
131
  toString(): string
52
132
  }
53
133
 
54
- export interface RSAPublicKey extends PublicKeyBase<'RSA', 0x12> {}
55
- export interface Ed25519PublicKey extends PublicKeyBase<'Ed25519', 0x0> {}
56
- export interface Secp256k1PublicKey extends PublicKeyBase<'secp256k1', 0x0> {}
57
134
  export type PublicKey = RSAPublicKey | Ed25519PublicKey | Secp256k1PublicKey
58
135
 
59
136
  /**
@@ -76,20 +153,75 @@ export function isPublicKey (key?: any): key is PublicKey {
76
153
  /**
77
154
  * Generic private key interface
78
155
  */
79
- interface PrivateKeyBase<KeyType extends string, PublicKeyType extends PublicKeyBase<KeyType>> {
156
+ export interface RSAPrivateKey {
157
+ /**
158
+ * The type of this key
159
+ */
160
+ readonly type: 'RSA'
161
+
162
+ /**
163
+ * The public key that corresponds to this private key
164
+ */
165
+ readonly publicKey: RSAPublicKey
166
+
167
+ /**
168
+ * PKIX in ASN1 DER format
169
+ */
170
+ readonly raw: Uint8Array
171
+
172
+ /**
173
+ * Returns `true` if the passed object matches this key
174
+ */
175
+ equals(key?: any): boolean
176
+
177
+ /**
178
+ * Sign the passed data with this private key and return the signature for
179
+ * later verification
180
+ */
181
+ sign(data: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array>
182
+ }
183
+
184
+ export interface Ed25519PrivateKey {
185
+ /**
186
+ * The type of this key
187
+ */
188
+ readonly type: 'Ed25519'
189
+
190
+ /**
191
+ * The public key that corresponds to this private key
192
+ */
193
+ readonly publicKey: Ed25519PublicKey
194
+
195
+ /**
196
+ * The raw public key bytes
197
+ */
198
+ readonly raw: Uint8Array
199
+
200
+ /**
201
+ * Returns `true` if the passed object matches this key
202
+ */
203
+ equals(key?: any): boolean
204
+
205
+ /**
206
+ * Sign the passed data with this private key and return the signature for
207
+ * later verification
208
+ */
209
+ sign(data: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array>
210
+ }
211
+
212
+ export interface Secp256k1PrivateKey {
80
213
  /**
81
214
  * The type of this key
82
215
  */
83
- readonly type: KeyType
216
+ readonly type: 'secp256k1'
84
217
 
85
218
  /**
86
219
  * The public key that corresponds to this private key
87
220
  */
88
- readonly publicKey: PublicKeyType
221
+ readonly publicKey: Secp256k1PublicKey
89
222
 
90
223
  /**
91
- * The raw public key bytes (for Ed25519 and secp256k1 keys) or PKIX in ASN1
92
- * DER format (for RSA keys)
224
+ * The raw public key bytes
93
225
  */
94
226
  readonly raw: Uint8Array
95
227
 
@@ -105,9 +237,6 @@ interface PrivateKeyBase<KeyType extends string, PublicKeyType extends PublicKey
105
237
  sign(data: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array>
106
238
  }
107
239
 
108
- export interface RSAPrivateKey extends PrivateKeyBase<'RSA', RSAPublicKey> {}
109
- export interface Ed25519PrivateKey extends PrivateKeyBase<'Ed25519', Ed25519PublicKey> {}
110
- export interface Secp256k1PrivateKey extends PrivateKeyBase<'secp256k1', Secp256k1PublicKey> {}
111
240
  export type PrivateKey = RSAPrivateKey | Ed25519PrivateKey | Secp256k1PrivateKey
112
241
 
113
242
  /**
@@ -142,6 +142,146 @@ export interface CounterGroup<T extends string = any> {
142
142
  reset(): void
143
143
  }
144
144
 
145
+ export interface HistogramOptions extends MetricOptions {
146
+ /**
147
+ * Buckets for the histogram
148
+ */
149
+ buckets?: number[]
150
+ }
151
+
152
+ /**
153
+ * Create tracked metrics that are expensive to calculate by passing
154
+ * a function that is only invoked when metrics are being scraped
155
+ */
156
+ export interface CalculatedHistogramOptions<T = number> extends HistogramOptions {
157
+ /**
158
+ * An optional function invoked to calculate the component metric instead of
159
+ * using `.observe`
160
+ */
161
+ calculate: CalculateMetric<T>
162
+ }
163
+
164
+ export interface Histogram {
165
+ /**
166
+ * Observe the passed value
167
+ */
168
+ observe(value: number): void
169
+
170
+ /**
171
+ * Reset this histogram to its default value
172
+ */
173
+ reset(): void
174
+
175
+ /**
176
+ * Start a timed metric, call the returned function to
177
+ * stop the timer
178
+ */
179
+ timer(): StopTimer
180
+ }
181
+
182
+ export interface HistogramGroup<T extends string = any> {
183
+ /**
184
+ * Observe the passed value for the named key in the group
185
+ */
186
+ observe(values: Partial<Record<T, number>>): void
187
+
188
+ /**
189
+ * Reset the passed key in this histogram group to its default value
190
+ * or all keys if no key is passed
191
+ */
192
+ reset(): void
193
+
194
+ /**
195
+ * Start a timed metric for the named key in the group, call
196
+ * the returned function to stop the timer
197
+ */
198
+ timer(key: string): StopTimer
199
+ }
200
+
201
+ export interface SummaryOptions extends MetricOptions {
202
+ /**
203
+ * Percentiles for the summary
204
+ */
205
+ percentiles?: number[]
206
+
207
+ /**
208
+ * Configure how old a bucket can be before it is reset for sliding window
209
+ */
210
+ maxAgeSeconds?: number
211
+
212
+ /**
213
+ * Configure how many buckets in the sliding window
214
+ */
215
+ ageBuckets?: number
216
+
217
+ /**
218
+ * Remove entries without any new values in the last `maxAgeSeconds`
219
+ */
220
+ pruneAgedBuckets?: boolean
221
+
222
+ /**
223
+ * Control compression of data in t-digest
224
+ */
225
+ compressCount?: number
226
+ }
227
+
228
+ /**
229
+ * Create tracked metrics that are expensive to calculate by passing
230
+ * a function that is only invoked when metrics are being scraped
231
+ */
232
+ export interface CalculatedSummaryOptions<T = number> extends SummaryOptions {
233
+ /**
234
+ * An optional function invoked to calculate the component metric instead of
235
+ * using `.observe`
236
+ */
237
+ calculate: CalculateMetric<T>
238
+ }
239
+
240
+ /**
241
+ * A tracked summary loosely based on the Summary interface exposed
242
+ * by the prom-client module
243
+ */
244
+ export interface Summary {
245
+ /**
246
+ * Observe the passed value
247
+ */
248
+ observe(value: number): void
249
+
250
+ /**
251
+ * Reset this summary to its default value
252
+ */
253
+ reset(): void
254
+
255
+ /**
256
+ * Start a timed metric, call the returned function to
257
+ * stop the timer
258
+ */
259
+ timer(): StopTimer
260
+ }
261
+
262
+ /**
263
+ * A group of tracked summaries loosely based on the Summary interface
264
+ * exposed by the prom-client module
265
+ */
266
+ export interface SummaryGroup<T extends string = any> {
267
+ /**
268
+ * Observe the passed value for the named key in the group
269
+ */
270
+ observe(values: Partial<Record<T, number>>): void
271
+
272
+ /**
273
+ * Reset the passed key in this summary group to its default value
274
+ * or all keys if no key is passed
275
+ */
276
+ reset(): void
277
+
278
+ /**
279
+ * Start a timed metric for the named key in the group, call
280
+ * the returned function to stop the timer
281
+ */
282
+ timer(key: string): StopTimer
283
+ }
284
+
145
285
  /**
146
286
  * The libp2p metrics tracking object. This interface is only concerned
147
287
  * with the collection of metrics, please see the individual implementations
@@ -322,4 +462,30 @@ export interface Metrics {
322
462
  * method on the returned counter group object
323
463
  */
324
464
  registerCounterGroup: ((name: string, options?: MetricOptions) => CounterGroup) & ((name: string, options: CalculatedMetricOptions<Record<string, number>>) => void)
465
+
466
+ /**
467
+ * Register an arbitrary histogram. Call this to set help/labels for histograms
468
+ * and observe them by calling methods on the returned histogram object
469
+ */
470
+ registerHistogram: ((name: string, options?: HistogramOptions) => Histogram) & ((name: string, options: CalculatedHistogramOptions) => void)
471
+
472
+ /**
473
+ * Register a a group of related histograms. Call this to set help/labels for
474
+ * groups of related histograms that will be updated with by calling the `.observe`
475
+ * method on the returned histogram group object
476
+ */
477
+ registerHistogramGroup: ((name: string, options?: HistogramOptions) => HistogramGroup) & ((name: string, options: CalculatedHistogramOptions<Record<string, number>>) => void)
478
+
479
+ /**
480
+ * Register an arbitrary summary. Call this to set help/labels for summaries
481
+ * and observe them by calling methods on the returned summary object
482
+ */
483
+ registerSummary: ((name: string, options?: SummaryOptions) => Summary) & ((name: string, options: CalculatedSummaryOptions) => void)
484
+
485
+ /**
486
+ * Register a a group of related summaries. Call this to set help/labels for
487
+ * groups of related summaries that will be updated with by calling the `.observe`
488
+ * method on the returned summary group object
489
+ */
490
+ registerSummaryGroup: ((name: string, options?: SummaryOptions) => SummaryGroup) & ((name: string, options: CalculatedSummaryOptions<Record<string, number>>) => void)
325
491
  }
@@ -115,7 +115,7 @@ export interface PubSubInit {
115
115
  maxOutboundStreams?: number
116
116
  }
117
117
 
118
- interface Subscription {
118
+ export interface Subscription {
119
119
  topic: string
120
120
  subscribe: boolean
121
121
  }
@@ -268,3 +268,16 @@ export interface PeerStreamEvents {
268
268
  'stream:outbound': CustomEvent<never>
269
269
  'close': CustomEvent<never>
270
270
  }
271
+
272
+ /**
273
+ * All Pubsub implementations must use this symbol as the name of a property
274
+ * with a boolean `true` value
275
+ */
276
+ export const pubSubSymbol = Symbol.for('@libp2p/pubsub')
277
+
278
+ /**
279
+ * Returns true if the passed argument is a PubSub implementation
280
+ */
281
+ export function isPubSub (obj?: any): obj is PubSub {
282
+ return Boolean(obj?.[pubSubSymbol])
283
+ }
@@ -11,12 +11,16 @@ export interface StreamHandler {
11
11
 
12
12
  export interface StreamHandlerOptions {
13
13
  /**
14
- * How many incoming streams can be open for this protocol at the same time on each connection (default: 32)
14
+ * How many incoming streams can be open for this protocol at the same time on each connection
15
+ *
16
+ * @default 32
15
17
  */
16
18
  maxInboundStreams?: number
17
19
 
18
20
  /**
19
- * How many outgoing streams can be open for this protocol at the same time on each connection (default: 64)
21
+ * How many outgoing streams can be open for this protocol at the same time on each connection
22
+ *
23
+ * @default 64
20
24
  */
21
25
  maxOutboundStreams?: number
22
26