@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.
- package/README.md +1 -1
- package/dist/src/connection/index.d.ts +13 -1
- package/dist/src/connection/index.d.ts.map +1 -1
- package/dist/src/connection/index.js.map +1 -1
- package/dist/src/errors.d.ts +4 -0
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +5 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/index.d.ts +35 -14
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/peer-info/index.d.ts +12 -1
- package/dist/src/peer-info/index.d.ts.map +1 -1
- package/dist/src/startable.d.ts +0 -1
- package/dist/src/startable.d.ts.map +1 -1
- package/dist/src/startable.js.map +1 -1
- package/dist/src/stream-muxer/stream.d.ts +2 -149
- package/dist/src/stream-muxer/stream.d.ts.map +1 -1
- package/dist/src/stream-muxer/stream.js +8 -322
- package/dist/src/stream-muxer/stream.js.map +1 -1
- package/dist/src/topology/index.d.ts +0 -2
- package/dist/src/topology/index.d.ts.map +1 -1
- package/package.json +10 -16
- package/src/connection/index.ts +16 -1
- package/src/errors.ts +7 -0
- package/src/index.ts +41 -15
- package/src/peer-info/index.ts +13 -1
- package/src/startable.ts +0 -2
- package/src/stream-muxer/stream.ts +9 -494
- package/src/topology/index.ts +0 -3
- package/dist/src/keychain/index.d.ts +0 -154
- package/dist/src/keychain/index.d.ts.map +0 -1
- package/dist/src/keychain/index.js +0 -23
- package/dist/src/keychain/index.js.map +0 -1
- package/dist/typedoc-urls.json +0 -262
- package/src/keychain/index.ts +0 -167
|
@@ -1,323 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
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,
|
|
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
|
|
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.
|
|
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/
|
|
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.
|
|
165
|
-
"
|
|
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.
|
|
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": "^
|
|
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
|
}
|
package/src/connection/index.ts
CHANGED
|
@@ -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
|
*
|
package/src/peer-info/index.ts
CHANGED
|
@@ -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
|
}
|