@libp2p/interface 0.1.5 → 0.1.6-0b4a2ee79

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.
@@ -1,323 +1,9 @@
1
- import { abortableSource } from 'abortable-iterator';
2
- import { pushable } from 'it-pushable';
3
- import defer, {} from 'p-defer';
4
- import { raceSignal } from 'race-signal';
5
- import { Uint8ArrayList } from 'uint8arraylist';
6
- import { CodeError } from '../errors.js';
7
- const ERR_STREAM_RESET = 'ERR_STREAM_RESET';
8
- const ERR_SINK_INVALID_STATE = 'ERR_SINK_INVALID_STATE';
9
- const DEFAULT_SEND_CLOSE_WRITE_TIMEOUT = 5000;
10
- function isPromise(res) {
11
- return res != null && typeof res.then === 'function';
12
- }
13
- export class AbstractStream {
14
- id;
15
- direction;
16
- timeline;
17
- protocol;
18
- metadata;
19
- source;
20
- status;
21
- readStatus;
22
- writeStatus;
23
- sinkController;
24
- sinkEnd;
25
- endErr;
26
- streamSource;
27
- onEnd;
28
- onCloseRead;
29
- onCloseWrite;
30
- onReset;
31
- onAbort;
32
- sendCloseWriteTimeout;
33
- log;
34
- constructor(init) {
35
- this.sinkController = new AbortController();
36
- this.sinkEnd = defer();
37
- this.log = init.log;
38
- // stream status
39
- this.status = 'open';
40
- this.readStatus = 'ready';
41
- this.writeStatus = 'ready';
42
- this.id = init.id;
43
- this.metadata = init.metadata ?? {};
44
- this.direction = init.direction;
45
- this.timeline = {
46
- open: Date.now()
47
- };
48
- this.sendCloseWriteTimeout = init.sendCloseWriteTimeout ?? DEFAULT_SEND_CLOSE_WRITE_TIMEOUT;
49
- this.onEnd = init.onEnd;
50
- this.onCloseRead = init?.onCloseRead;
51
- this.onCloseWrite = init?.onCloseWrite;
52
- this.onReset = init?.onReset;
53
- this.onAbort = init?.onAbort;
54
- this.source = this.streamSource = pushable({
55
- onEnd: (err) => {
56
- if (err != null) {
57
- this.log.trace('source ended with error', err);
58
- }
59
- else {
60
- this.log.trace('source ended');
61
- }
62
- this.onSourceEnd(err);
63
- }
64
- });
65
- // necessary because the libp2p upgrader wraps the sink function
66
- this.sink = this.sink.bind(this);
67
- }
68
- async sink(source) {
69
- if (this.writeStatus !== 'ready') {
70
- throw new CodeError(`writable end state is "${this.writeStatus}" not "ready"`, ERR_SINK_INVALID_STATE);
71
- }
72
- try {
73
- this.writeStatus = 'writing';
74
- const options = {
75
- signal: this.sinkController.signal
76
- };
77
- if (this.direction === 'outbound') { // If initiator, open a new stream
78
- const res = this.sendNewStream(options);
79
- if (isPromise(res)) {
80
- await res;
81
- }
82
- }
83
- source = abortableSource(source, this.sinkController.signal, {
84
- returnOnAbort: true
85
- });
86
- this.log.trace('sink reading from source');
87
- for await (let data of source) {
88
- data = data instanceof Uint8Array ? new Uint8ArrayList(data) : data;
89
- const res = this.sendData(data, options);
90
- if (isPromise(res)) { // eslint-disable-line max-depth
91
- await res;
92
- }
93
- }
94
- this.log.trace('sink finished reading from source, write status is "%s"', this.writeStatus);
95
- if (this.writeStatus === 'writing') {
96
- this.writeStatus = 'closing';
97
- this.log.trace('send close write to remote');
98
- await this.sendCloseWrite({
99
- signal: AbortSignal.timeout(this.sendCloseWriteTimeout)
100
- });
101
- this.writeStatus = 'closed';
102
- }
103
- this.onSinkEnd();
104
- }
105
- catch (err) {
106
- this.log.trace('sink ended with error, calling abort with error', err);
107
- this.abort(err);
108
- throw err;
109
- }
110
- finally {
111
- this.log.trace('resolve sink end');
112
- this.sinkEnd.resolve();
113
- }
114
- }
115
- onSourceEnd(err) {
116
- if (this.timeline.closeRead != null) {
117
- return;
118
- }
119
- this.timeline.closeRead = Date.now();
120
- this.readStatus = 'closed';
121
- if (err != null && this.endErr == null) {
122
- this.endErr = err;
123
- }
124
- this.onCloseRead?.();
125
- if (this.timeline.closeWrite != null) {
126
- this.log.trace('source and sink ended');
127
- this.timeline.close = Date.now();
128
- if (this.status !== 'aborted' && this.status !== 'reset') {
129
- this.status = 'closed';
130
- }
131
- if (this.onEnd != null) {
132
- this.onEnd(this.endErr);
133
- }
134
- }
135
- else {
136
- this.log.trace('source ended, waiting for sink to end');
137
- }
138
- }
139
- onSinkEnd(err) {
140
- if (this.timeline.closeWrite != null) {
141
- return;
142
- }
143
- this.timeline.closeWrite = Date.now();
144
- this.writeStatus = 'closed';
145
- if (err != null && this.endErr == null) {
146
- this.endErr = err;
147
- }
148
- this.onCloseWrite?.();
149
- if (this.timeline.closeRead != null) {
150
- this.log.trace('sink and source ended');
151
- this.timeline.close = Date.now();
152
- if (this.status !== 'aborted' && this.status !== 'reset') {
153
- this.status = 'closed';
154
- }
155
- if (this.onEnd != null) {
156
- this.onEnd(this.endErr);
157
- }
158
- }
159
- else {
160
- this.log.trace('sink ended, waiting for source to end');
161
- }
162
- }
163
- // Close for both Reading and Writing
164
- async close(options) {
165
- this.log.trace('closing gracefully');
166
- this.status = 'closing';
167
- await Promise.all([
168
- this.closeRead(options),
169
- this.closeWrite(options)
170
- ]);
171
- this.status = 'closed';
172
- this.log.trace('closed gracefully');
173
- }
174
- async closeRead(options = {}) {
175
- if (this.readStatus === 'closing' || this.readStatus === 'closed') {
176
- return;
177
- }
178
- this.log.trace('closing readable end of stream with starting read status "%s"', this.readStatus);
179
- const readStatus = this.readStatus;
180
- this.readStatus = 'closing';
181
- if (this.status !== 'reset' && this.status !== 'aborted' && this.timeline.closeRead == null) {
182
- this.log.trace('send close read to remote');
183
- await this.sendCloseRead(options);
184
- }
185
- if (readStatus === 'ready') {
186
- this.log.trace('ending internal source queue');
187
- this.streamSource.end();
188
- }
189
- this.log.trace('closed readable end of stream');
190
- }
191
- async closeWrite(options = {}) {
192
- if (this.writeStatus === 'closing' || this.writeStatus === 'closed') {
193
- return;
194
- }
195
- this.log.trace('closing writable end of stream with starting write status "%s"', this.writeStatus);
196
- if (this.writeStatus === 'ready') {
197
- this.log.trace('sink was never sunk, sink an empty array');
198
- await raceSignal(this.sink([]), options.signal);
199
- }
200
- if (this.writeStatus === 'writing') {
201
- // stop reading from the source passed to `.sink` in the microtask queue
202
- // - this lets any data queued by the user in the current tick get read
203
- // before we exit
204
- await new Promise((resolve, reject) => {
205
- queueMicrotask(() => {
206
- this.log.trace('aborting source passed to .sink');
207
- this.sinkController.abort();
208
- raceSignal(this.sinkEnd.promise, options.signal)
209
- .then(resolve, reject);
210
- });
211
- });
212
- }
213
- this.writeStatus = 'closed';
214
- this.log.trace('closed writable end of stream');
215
- }
216
- /**
217
- * Close immediately for reading and writing and send a reset message (local
218
- * error)
219
- */
220
- abort(err) {
221
- if (this.status === 'closed' || this.status === 'aborted' || this.status === 'reset') {
222
- return;
223
- }
224
- this.log('abort with error', err);
225
- // try to send a reset message
226
- this.log('try to send reset to remote');
227
- const res = this.sendReset();
228
- if (isPromise(res)) {
229
- res.catch((err) => {
230
- this.log.error('error sending reset message', err);
231
- });
232
- }
233
- this.status = 'aborted';
234
- this.timeline.abort = Date.now();
235
- this._closeSinkAndSource(err);
236
- this.onAbort?.(err);
237
- }
238
- /**
239
- * Receive a reset message - close immediately for reading and writing (remote
240
- * error)
241
- */
242
- reset() {
243
- if (this.status === 'closed' || this.status === 'aborted' || this.status === 'reset') {
244
- return;
245
- }
246
- const err = new CodeError('stream reset', ERR_STREAM_RESET);
247
- this.status = 'reset';
248
- this.timeline.reset = Date.now();
249
- this._closeSinkAndSource(err);
250
- this.onReset?.();
251
- }
252
- _closeSinkAndSource(err) {
253
- this._closeSink(err);
254
- this._closeSource(err);
255
- }
256
- _closeSink(err) {
257
- // if the sink function is running, cause it to end
258
- if (this.writeStatus === 'writing') {
259
- this.log.trace('end sink source');
260
- this.sinkController.abort();
261
- }
262
- this.onSinkEnd(err);
263
- }
264
- _closeSource(err) {
265
- // if the source is not ending, end it
266
- if (this.readStatus !== 'closing' && this.readStatus !== 'closed') {
267
- this.log.trace('ending source with %d bytes to be read by consumer', this.streamSource.readableLength);
268
- this.readStatus = 'closing';
269
- this.streamSource.end(err);
270
- }
271
- }
272
- /**
273
- * The remote closed for writing so we should expect to receive no more
274
- * messages
275
- */
276
- remoteCloseWrite() {
277
- if (this.readStatus === 'closing' || this.readStatus === 'closed') {
278
- this.log('received remote close write but local source is already closed');
279
- return;
280
- }
281
- this.log.trace('remote close write');
282
- this._closeSource();
283
- }
284
- /**
285
- * The remote closed for reading so we should not send any more
286
- * messages
287
- */
288
- remoteCloseRead() {
289
- if (this.writeStatus === 'closing' || this.writeStatus === 'closed') {
290
- this.log('received remote close read but local sink is already closed');
291
- return;
292
- }
293
- this.log.trace('remote close read');
294
- this._closeSink();
295
- }
296
- /**
297
- * The underlying muxer has closed, no more messages can be sent or will
298
- * be received, close immediately to free up resources
299
- */
300
- destroy() {
301
- if (this.status === 'closed' || this.status === 'aborted' || this.status === 'reset') {
302
- this.log('received destroy but we are already closed');
303
- return;
304
- }
305
- this.log.trace('stream destroyed');
306
- this._closeSinkAndSource();
307
- }
308
- /**
309
- * When an extending class reads data from it's implementation-specific source,
310
- * call this method to allow the stream consumer to read the data.
311
- */
312
- sourcePush(data) {
313
- this.streamSource.push(data);
314
- }
315
- /**
316
- * Returns the amount of unread data - can be used to prevent large amounts of
317
- * data building up when the stream consumer is too slow.
318
- */
319
- sourceReadableLength() {
320
- return this.streamSource.readableLength;
321
- }
322
- }
1
+ // TODO: remove this file and the dep-check override in package.json for
2
+ // @libp2p/utils when yamux is updated.
3
+ // This is a hack to defeat TypeScript trying to inspect the types for
4
+ // @libp2p/utils/abstract-stream which depends on this module - making the
5
+ // import path only resolvable at runtime breaks the transpile time circular
6
+ // dependency
7
+ const s = await import('@libp2p/utils' + '/abstract-stream');
8
+ export const AbstractStream = s.AbstractStream;
323
9
  //# sourceMappingURL=stream.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"stream.js","sourceRoot":"","sources":["../../../src/stream-muxer/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAiB,QAAQ,EAAE,MAAM,aAAa,CAAA;AACrD,OAAO,KAAK,EAA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;;OAGG;IACH,UAAU,CAAE,IAAoB;QAC9B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC9B,CAAC;IAED;;;OAGG;IACH,oBAAoB;QAClB,OAAO,IAAI,CAAC,YAAY,CAAC,cAAc,CAAA;IACzC,CAAC;CA6BF"}
1
+ {"version":3,"file":"stream.js","sourceRoot":"","sources":["../../../src/stream-muxer/stream.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,uCAAuC;AACvC,sEAAsE;AACtE,0EAA0E;AAC1E,4EAA4E;AAC5E,aAAa;AACb,MAAM,CAAC,GAAG,MAAM,MAAM,CAAC,eAAe,GAAG,kBAAkB,CAAC,CAAA;AAI5D,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,cAAc,CAAA"}
@@ -1,8 +1,6 @@
1
1
  import type { Connection } from '../connection/index.js';
2
2
  import type { PeerId } from '../peer-id/index.js';
3
3
  export interface Topology {
4
- min?: number;
5
- max?: number;
6
4
  /**
7
5
  * If true, invoke `onConnect` for this topology on transient (e.g. short-lived
8
6
  * and/or data-limited) connections. (default: false)
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/topology/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAEjD,MAAM,WAAW,QAAQ;IACvB,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,MAAM,CAAA;IAEZ;;;OAGG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAE3B;;;OAGG;IACH,SAAS,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,IAAI,CAAA;IAElD;;;OAGG;IACH,YAAY,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CACpC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/topology/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAEjD,MAAM,WAAW,QAAQ;IACvB;;;OAGG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAE3B;;;OAGG;IACH,SAAS,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,IAAI,CAAA;IAElD;;;OAGG;IACH,YAAY,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CACpC"}
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@libp2p/interface",
3
- "version": "0.1.5",
3
+ "version": "0.1.6-0b4a2ee79",
4
4
  "description": "The interface implemented by a libp2p node",
5
5
  "license": "Apache-2.0 OR MIT",
6
- "homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/interface#readme",
6
+ "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/interface#readme",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "git+https://github.com/libp2p/js-libp2p.git"
@@ -39,9 +39,6 @@
39
39
  "!dist/test",
40
40
  "!**/*.tsbuildinfo"
41
41
  ],
42
- "browser": {
43
- "events": "./dist/src/events.browser.js"
44
- },
45
42
  "exports": {
46
43
  ".": {
47
44
  "types": "./dist/src/index.d.ts",
@@ -150,7 +147,7 @@
150
147
  "scripts": {
151
148
  "clean": "aegir clean",
152
149
  "lint": "aegir lint",
153
- "dep-check": "aegir dep-check",
150
+ "dep-check": "aegir dep-check -i @libp2p/utils",
154
151
  "build": "aegir build",
155
152
  "test": "aegir test",
156
153
  "test:chrome": "aegir test -t browser --cov",
@@ -161,22 +158,19 @@
161
158
  "test:electron-main": "aegir test -t electron-main"
162
159
  },
163
160
  "dependencies": {
164
- "@multiformats/multiaddr": "^12.1.5",
165
- "abortable-iterator": "^5.0.1",
166
- "it-pushable": "^3.2.0",
161
+ "@multiformats/multiaddr": "^12.1.10",
162
+ "it-pushable": "^3.2.1",
167
163
  "it-stream-types": "^2.0.1",
168
- "multiformats": "^12.0.1",
169
- "p-defer": "^4.0.0",
170
- "race-signal": "^1.0.0",
164
+ "multiformats": "^12.1.3",
171
165
  "uint8arraylist": "^2.4.3"
172
166
  },
173
167
  "devDependencies": {
174
- "@types/sinon": "^10.0.15",
168
+ "@types/sinon": "^17.0.0",
175
169
  "aegir": "^41.0.2",
176
- "delay": "^6.0.0",
177
- "it-all": "^3.0.3",
178
- "it-drain": "^3.0.3",
179
170
  "sinon": "^17.0.0",
180
171
  "sinon-ts": "^2.0.0"
172
+ },
173
+ "browser": {
174
+ "events": "./dist/src/events.browser.js"
181
175
  }
182
176
  }
@@ -1,4 +1,4 @@
1
- import type { AbortOptions } from '../index.js'
1
+ import type { AbortOptions, Logger } from '../index.js'
2
2
  import type { PeerId } from '../peer-id/index.js'
3
3
  import type { Multiaddr } from '@multiformats/multiaddr'
4
4
  import type { Duplex, Source } from 'it-stream-types'
@@ -169,6 +169,11 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
169
169
  * The current status of the writable end of the stream
170
170
  */
171
171
  writeStatus: WriteStatus
172
+
173
+ /**
174
+ * The stream logger
175
+ */
176
+ log: Logger
172
177
  }
173
178
 
174
179
  export interface NewStreamOptions extends AbortOptions {
@@ -268,6 +273,11 @@ export interface Connection {
268
273
  * Immediately close the connection, any queued data will be discarded
269
274
  */
270
275
  abort(err: Error): void
276
+
277
+ /**
278
+ * The connection logger
279
+ */
280
+ log: Logger
271
281
  }
272
282
 
273
283
  export const symbol = Symbol.for('@libp2p/connection')
@@ -329,4 +339,9 @@ export interface MultiaddrConnection extends Duplex<AsyncGenerator<Uint8Array>,
329
339
  * When connection lifecycle events occurred
330
340
  */
331
341
  timeline: MultiaddrConnectionTimeline
342
+
343
+ /**
344
+ * The multiaddr connection logger
345
+ */
346
+ log: Logger
332
347
  }
package/src/errors.ts CHANGED
@@ -65,3 +65,10 @@ export class InvalidCryptoTransmissionError extends Error {
65
65
 
66
66
  static readonly code = 'ERR_INVALID_CRYPTO_TRANSMISSION'
67
67
  }
68
+
69
+ // Error codes
70
+
71
+ export const ERR_TIMEOUT = 'ERR_TIMEOUT'
72
+ export const ERR_INVALID_PARAMETERS = 'ERR_INVALID_PARAMETERS'
73
+ export const ERR_NOT_FOUND = 'ERR_NOT_FOUND'
74
+ export const ERR_INVALID_MESSAGE = 'ERR_INVALID_MESSAGE'
package/src/index.ts CHANGED
@@ -17,7 +17,6 @@
17
17
  import type { Connection, NewStreamOptions, Stream } from './connection/index.js'
18
18
  import type { ContentRouting } from './content-routing/index.js'
19
19
  import type { TypedEventTarget } from './events.js'
20
- import type { KeyChain } from './keychain/index.js'
21
20
  import type { Metrics } from './metrics/index.js'
22
21
  import type { PeerId } from './peer-id/index.js'
23
22
  import type { PeerInfo } from './peer-info/index.js'
@@ -106,6 +105,23 @@ export interface IdentifyResult {
106
105
  connection: Connection
107
106
  }
108
107
 
108
+ /**
109
+ * Logger component for libp2p
110
+ */
111
+ export interface Logger {
112
+ (formatter: any, ...args: any[]): void
113
+ error(formatter: any, ...args: any[]): void
114
+ trace(formatter: any, ...args: any[]): void
115
+ enabled: boolean
116
+ }
117
+
118
+ /**
119
+ * Peer logger component for libp2p
120
+ */
121
+ export interface ComponentLogger {
122
+ forComponent(name: string): Logger
123
+ }
124
+
109
125
  /**
110
126
  * Once you have a libp2p instance, you can listen to several events it emits,
111
127
  * so that you can be notified of relevant network events.
@@ -377,20 +393,6 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ty
377
393
  */
378
394
  contentRouting: ContentRouting
379
395
 
380
- /**
381
- * The keychain contains the keys used by the current node, and can create new
382
- * keys, export them, import them, etc.
383
- *
384
- * @example
385
- *
386
- * ```js
387
- * const keyInfo = await libp2p.keychain.createKey('new key')
388
- * console.info(keyInfo)
389
- * // { id: '...', name: 'new key' }
390
- * ```
391
- */
392
- keychain: KeyChain
393
-
394
396
  /**
395
397
  * The metrics subsystem allows recording values to assess the health/performance
396
398
  * of the running node.
@@ -408,6 +410,8 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ty
408
410
  */
409
411
  metrics?: Metrics
410
412
 
413
+ logger: ComponentLogger
414
+
411
415
  /**
412
416
  * Get a deduplicated list of peer advertising multiaddrs by concatenating
413
417
  * the listen addresses used by transports with any configured
@@ -605,6 +609,21 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ty
605
609
  services: T
606
610
  }
607
611
 
612
+ /**
613
+ * Metadata about the current node
614
+ */
615
+ export interface NodeInfo {
616
+ /**
617
+ * The implementation name
618
+ */
619
+ name: string
620
+
621
+ /**
622
+ * The implementation version
623
+ */
624
+ version: string
625
+ }
626
+
608
627
  /**
609
628
  * An object that contains an AbortSignal as
610
629
  * the optional `signal` property.
@@ -626,6 +645,13 @@ export interface AbortOptions {
626
645
  signal?: AbortSignal
627
646
  }
628
647
 
648
+ /**
649
+ * An object that contains a Logger as the `log` property.
650
+ */
651
+ export interface LoggerOptions {
652
+ log: Logger
653
+ }
654
+
629
655
  /**
630
656
  * Returns a new type with all fields marked optional.
631
657
  *
@@ -1,8 +1,20 @@
1
1
  import type { PeerId } from '../peer-id/index.js'
2
2
  import type { Multiaddr } from '@multiformats/multiaddr'
3
3
 
4
+ /**
5
+ * A `PeerInfo` is a lightweight object that represents a remote peer, it can be
6
+ * obtained from peer discovery mechanisms, HTTP RPC endpoints, etc.
7
+ *
8
+ * @see https://docs.libp2p.io/concepts/fundamentals/peers/#peer-info
9
+ */
4
10
  export interface PeerInfo {
11
+ /**
12
+ * The identifier of the remote peer
13
+ */
5
14
  id: PeerId
15
+
16
+ /**
17
+ * The multiaddrs a peer is listening on
18
+ */
6
19
  multiaddrs: Multiaddr[]
7
- protocols: string[]
8
20
  }
package/src/startable.ts CHANGED
@@ -2,8 +2,6 @@
2
2
  * Implemented by components that have a lifecycle
3
3
  */
4
4
  export interface Startable {
5
- isStarted(): boolean
6
-
7
5
  /**
8
6
  * If implemented, this method will be invoked before the start method.
9
7
  *