@libp2p/interface 0.0.1-05abd49f
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/LICENSE +4 -0
- package/README.md +45 -0
- package/dist/index.min.js +3 -0
- package/dist/src/connection/index.d.ts +188 -0
- package/dist/src/connection/index.d.ts.map +1 -0
- package/dist/src/connection/index.js +5 -0
- package/dist/src/connection/index.js.map +1 -0
- package/dist/src/connection/status.d.ts +4 -0
- package/dist/src/connection/status.d.ts.map +1 -0
- package/dist/src/connection/status.js +4 -0
- package/dist/src/connection/status.js.map +1 -0
- package/dist/src/connection-encrypter/index.d.ts +27 -0
- package/dist/src/connection-encrypter/index.d.ts.map +1 -0
- package/dist/src/connection-encrypter/index.js +2 -0
- package/dist/src/connection-encrypter/index.js.map +1 -0
- package/dist/src/connection-gater/index.d.ts +117 -0
- package/dist/src/connection-gater/index.d.ts.map +1 -0
- package/dist/src/connection-gater/index.js +2 -0
- package/dist/src/connection-gater/index.js.map +1 -0
- package/dist/src/content-routing/index.d.ts +79 -0
- package/dist/src/content-routing/index.d.ts.map +1 -0
- package/dist/src/content-routing/index.js +21 -0
- package/dist/src/content-routing/index.js.map +1 -0
- package/dist/src/errors.d.ts +33 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +51 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/events.d.ts +28 -0
- package/dist/src/events.d.ts.map +1 -0
- package/dist/src/events.js +70 -0
- package/dist/src/events.js.map +1 -0
- package/dist/src/index.d.ts +575 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +17 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/keychain/index.d.ts +154 -0
- package/dist/src/keychain/index.d.ts.map +1 -0
- package/dist/src/keychain/index.js +23 -0
- package/dist/src/keychain/index.js.map +1 -0
- package/dist/src/keys/index.d.ts +35 -0
- package/dist/src/keys/index.d.ts.map +1 -0
- package/dist/src/keys/index.js +4 -0
- package/dist/src/keys/index.js.map +1 -0
- package/dist/src/metrics/index.d.ts +165 -0
- package/dist/src/metrics/index.d.ts.map +1 -0
- package/dist/src/metrics/index.js +2 -0
- package/dist/src/metrics/index.js.map +1 -0
- package/dist/src/metrics/tracked-map.d.ts +17 -0
- package/dist/src/metrics/tracked-map.d.ts.map +1 -0
- package/dist/src/metrics/tracked-map.js +38 -0
- package/dist/src/metrics/tracked-map.js.map +1 -0
- package/dist/src/peer-discovery/index.d.ts +28 -0
- package/dist/src/peer-discovery/index.d.ts.map +1 -0
- package/dist/src/peer-discovery/index.js +21 -0
- package/dist/src/peer-discovery/index.js.map +1 -0
- package/dist/src/peer-id/index.d.ts +30 -0
- package/dist/src/peer-id/index.d.ts.map +1 -0
- package/dist/src/peer-id/index.js +5 -0
- package/dist/src/peer-id/index.js.map +1 -0
- package/dist/src/peer-info/index.d.ts +8 -0
- package/dist/src/peer-info/index.d.ts.map +1 -0
- package/dist/src/peer-info/index.js +2 -0
- package/dist/src/peer-info/index.js.map +1 -0
- package/dist/src/peer-routing/index.d.ts +51 -0
- package/dist/src/peer-routing/index.d.ts.map +1 -0
- package/dist/src/peer-routing/index.js +21 -0
- package/dist/src/peer-routing/index.js.map +1 -0
- package/dist/src/peer-store/index.d.ts +247 -0
- package/dist/src/peer-store/index.d.ts.map +1 -0
- package/dist/src/peer-store/index.js +2 -0
- package/dist/src/peer-store/index.js.map +1 -0
- package/dist/src/peer-store/tags.d.ts +2 -0
- package/dist/src/peer-store/tags.d.ts.map +1 -0
- package/dist/src/peer-store/tags.js +2 -0
- package/dist/src/peer-store/tags.js.map +1 -0
- package/dist/src/record/index.d.ts +33 -0
- package/dist/src/record/index.d.ts.map +1 -0
- package/dist/src/record/index.js +2 -0
- package/dist/src/record/index.js.map +1 -0
- package/dist/src/startable.d.ts +46 -0
- package/dist/src/startable.d.ts.map +1 -0
- package/dist/src/startable.js +46 -0
- package/dist/src/startable.js.map +1 -0
- package/dist/src/stream-handler/index.d.ts +23 -0
- package/dist/src/stream-handler/index.d.ts.map +1 -0
- package/dist/src/stream-handler/index.js +2 -0
- package/dist/src/stream-handler/index.js.map +1 -0
- package/dist/src/stream-muxer/index.d.ts +51 -0
- package/dist/src/stream-muxer/index.d.ts.map +1 -0
- package/dist/src/stream-muxer/index.js +2 -0
- package/dist/src/stream-muxer/index.js.map +1 -0
- package/dist/src/stream-muxer/stream.d.ts +85 -0
- package/dist/src/stream-muxer/stream.d.ts.map +1 -0
- package/dist/src/stream-muxer/stream.js +255 -0
- package/dist/src/stream-muxer/stream.js.map +1 -0
- package/dist/src/topology/index.d.ts +9 -0
- package/dist/src/topology/index.d.ts.map +1 -0
- package/dist/src/topology/index.js +2 -0
- package/dist/src/topology/index.js.map +1 -0
- package/dist/src/transport/index.d.ts +96 -0
- package/dist/src/transport/index.d.ts.map +1 -0
- package/dist/src/transport/index.js +19 -0
- package/dist/src/transport/index.js.map +1 -0
- package/package.json +177 -0
- package/src/connection/index.ts +221 -0
- package/src/connection/status.ts +4 -0
- package/src/connection-encrypter/index.ts +30 -0
- package/src/connection-gater/index.ts +128 -0
- package/src/content-routing/index.ts +83 -0
- package/src/errors.ts +68 -0
- package/src/events.ts +100 -0
- package/src/index.ts +627 -0
- package/src/keychain/index.ts +167 -0
- package/src/keys/index.ts +38 -0
- package/src/metrics/index.ts +187 -0
- package/src/metrics/tracked-map.ts +65 -0
- package/src/peer-discovery/index.ts +29 -0
- package/src/peer-id/index.ts +39 -0
- package/src/peer-info/index.ts +8 -0
- package/src/peer-routing/index.ts +53 -0
- package/src/peer-store/index.ts +272 -0
- package/src/peer-store/tags.ts +2 -0
- package/src/record/index.ts +35 -0
- package/src/startable.ts +117 -0
- package/src/stream-handler/index.ts +27 -0
- package/src/stream-muxer/index.ts +58 -0
- package/src/stream-muxer/stream.ts +365 -0
- package/src/topology/index.ts +10 -0
- package/src/transport/index.ts +111 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
2
|
+
import type { Direction, Stream, StreamStat } from '../connection/index.js';
|
|
3
|
+
import type { Source } from 'it-stream-types';
|
|
4
|
+
export interface AbstractStreamInit {
|
|
5
|
+
/**
|
|
6
|
+
* A unique identifier for this stream
|
|
7
|
+
*/
|
|
8
|
+
id: string;
|
|
9
|
+
/**
|
|
10
|
+
* The stream direction
|
|
11
|
+
*/
|
|
12
|
+
direction: Direction;
|
|
13
|
+
/**
|
|
14
|
+
* The maximum allowable data size, any data larger than this will be
|
|
15
|
+
* chunked and sent in multiple data messages
|
|
16
|
+
*/
|
|
17
|
+
maxDataSize: number;
|
|
18
|
+
/**
|
|
19
|
+
* User specific stream metadata
|
|
20
|
+
*/
|
|
21
|
+
metadata?: Record<string, unknown>;
|
|
22
|
+
/**
|
|
23
|
+
* Invoked when the stream ends
|
|
24
|
+
*/
|
|
25
|
+
onEnd?: (err?: Error | undefined) => void;
|
|
26
|
+
}
|
|
27
|
+
export declare abstract class AbstractStream implements Stream {
|
|
28
|
+
id: string;
|
|
29
|
+
stat: StreamStat;
|
|
30
|
+
metadata: Record<string, unknown>;
|
|
31
|
+
source: AsyncGenerator<Uint8ArrayList, void, unknown>;
|
|
32
|
+
private readonly abortController;
|
|
33
|
+
private readonly resetController;
|
|
34
|
+
private readonly closeController;
|
|
35
|
+
private sourceEnded;
|
|
36
|
+
private sinkEnded;
|
|
37
|
+
private sinkSunk;
|
|
38
|
+
private endErr;
|
|
39
|
+
private readonly streamSource;
|
|
40
|
+
private readonly onEnd?;
|
|
41
|
+
private readonly maxDataSize;
|
|
42
|
+
constructor(init: AbstractStreamInit);
|
|
43
|
+
protected onSourceEnd(err?: Error): void;
|
|
44
|
+
protected onSinkEnd(err?: Error): void;
|
|
45
|
+
close(): void;
|
|
46
|
+
closeRead(): void;
|
|
47
|
+
closeWrite(): void;
|
|
48
|
+
abort(err: Error): void;
|
|
49
|
+
reset(): void;
|
|
50
|
+
sink(source: Source<Uint8ArrayList | Uint8Array>): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* When an extending class reads data from it's implementation-specific source,
|
|
53
|
+
* call this method to allow the stream consumer to read the data.
|
|
54
|
+
*/
|
|
55
|
+
sourcePush(data: Uint8ArrayList): void;
|
|
56
|
+
/**
|
|
57
|
+
* Returns the amount of unread data - can be used to prevent large amounts of
|
|
58
|
+
* data building up when the stream consumer is too slow.
|
|
59
|
+
*/
|
|
60
|
+
sourceReadableLength(): number;
|
|
61
|
+
/**
|
|
62
|
+
* Send a message to the remote muxer informing them a new stream is being
|
|
63
|
+
* opened
|
|
64
|
+
*/
|
|
65
|
+
abstract sendNewStream(): void | Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Send a data message to the remote muxer
|
|
68
|
+
*/
|
|
69
|
+
abstract sendData(buf: Uint8ArrayList): void | Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Send a reset message to the remote muxer
|
|
72
|
+
*/
|
|
73
|
+
abstract sendReset(): void | Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* Send a message to the remote muxer, informing them no more data messages
|
|
76
|
+
* will be sent by this end of the stream
|
|
77
|
+
*/
|
|
78
|
+
abstract sendCloseWrite(): void | Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Send a message to the remote muxer, informing them no more data messages
|
|
81
|
+
* will be read by this end of the stream
|
|
82
|
+
*/
|
|
83
|
+
abstract sendCloseRead(): void | Promise<void>;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../../src/stream-muxer/stream.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAE/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AAC3E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAa7C,MAAM,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K,GAAG,IAAI;IASxB,KAAK,IAAK,IAAI;IAOR,IAAI,CAAE,MAAM,EAAE,MAAM,CAAC,cAAc,GAAG,UAAU,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAyGvE;;;OAGG;IACH,UAAU,CAAE,IAAI,EAAE,cAAc,GAAG,IAAI;IAIvC;;;OAGG;IACH,oBAAoB,IAAK,MAAM;IAI/B;;;OAGG;IACH,QAAQ,CAAC,aAAa,IAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAE/C;;OAEG;IACH,QAAQ,CAAC,QAAQ,CAAE,GAAG,EAAE,cAAc,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAE7D;;OAEG;IACH,QAAQ,CAAC,SAAS,IAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAE3C;;;OAGG;IACH,QAAQ,CAAC,cAAc,IAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAEhD;;;OAGG;IACH,QAAQ,CAAC,aAAa,IAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;CAChD"}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// import { logger } from '@libp2p/logger'
|
|
2
|
+
import { abortableSource } from 'abortable-iterator';
|
|
3
|
+
import { anySignal } from 'any-signal';
|
|
4
|
+
import { pushable } from 'it-pushable';
|
|
5
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
6
|
+
import { CodeError } from '../errors.js';
|
|
7
|
+
// const log = logger('libp2p:stream')
|
|
8
|
+
const log = () => { };
|
|
9
|
+
log.trace = () => { };
|
|
10
|
+
log.error = () => { };
|
|
11
|
+
const ERR_STREAM_RESET = 'ERR_STREAM_RESET';
|
|
12
|
+
const ERR_STREAM_ABORT = 'ERR_STREAM_ABORT';
|
|
13
|
+
const ERR_SINK_ENDED = 'ERR_SINK_ENDED';
|
|
14
|
+
const ERR_DOUBLE_SINK = 'ERR_DOUBLE_SINK';
|
|
15
|
+
function isPromise(res) {
|
|
16
|
+
return res != null && typeof res.then === 'function';
|
|
17
|
+
}
|
|
18
|
+
export class AbstractStream {
|
|
19
|
+
id;
|
|
20
|
+
stat;
|
|
21
|
+
metadata;
|
|
22
|
+
source;
|
|
23
|
+
abortController;
|
|
24
|
+
resetController;
|
|
25
|
+
closeController;
|
|
26
|
+
sourceEnded;
|
|
27
|
+
sinkEnded;
|
|
28
|
+
sinkSunk;
|
|
29
|
+
endErr;
|
|
30
|
+
streamSource;
|
|
31
|
+
onEnd;
|
|
32
|
+
maxDataSize;
|
|
33
|
+
constructor(init) {
|
|
34
|
+
this.abortController = new AbortController();
|
|
35
|
+
this.resetController = new AbortController();
|
|
36
|
+
this.closeController = new AbortController();
|
|
37
|
+
this.sourceEnded = false;
|
|
38
|
+
this.sinkEnded = false;
|
|
39
|
+
this.sinkSunk = false;
|
|
40
|
+
this.id = init.id;
|
|
41
|
+
this.metadata = init.metadata ?? {};
|
|
42
|
+
this.stat = {
|
|
43
|
+
direction: init.direction,
|
|
44
|
+
timeline: {
|
|
45
|
+
open: Date.now()
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
this.maxDataSize = init.maxDataSize;
|
|
49
|
+
this.onEnd = init.onEnd;
|
|
50
|
+
this.source = this.streamSource = pushable({
|
|
51
|
+
onEnd: () => {
|
|
52
|
+
// already sent a reset message
|
|
53
|
+
if (this.stat.timeline.reset !== null) {
|
|
54
|
+
const res = this.sendCloseRead();
|
|
55
|
+
if (isPromise(res)) {
|
|
56
|
+
res.catch(err => {
|
|
57
|
+
log.error('error while sending close read', err);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
this.onSourceEnd();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
// necessary because the libp2p upgrader wraps the sink function
|
|
65
|
+
this.sink = this.sink.bind(this);
|
|
66
|
+
}
|
|
67
|
+
onSourceEnd(err) {
|
|
68
|
+
if (this.sourceEnded) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
this.stat.timeline.closeRead = Date.now();
|
|
72
|
+
this.sourceEnded = true;
|
|
73
|
+
log.trace('%s stream %s source end - err: %o', this.stat.direction, this.id, err);
|
|
74
|
+
if (err != null && this.endErr == null) {
|
|
75
|
+
this.endErr = err;
|
|
76
|
+
}
|
|
77
|
+
if (this.sinkEnded) {
|
|
78
|
+
this.stat.timeline.close = Date.now();
|
|
79
|
+
if (this.onEnd != null) {
|
|
80
|
+
this.onEnd(this.endErr);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
onSinkEnd(err) {
|
|
85
|
+
if (this.sinkEnded) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
this.stat.timeline.closeWrite = Date.now();
|
|
89
|
+
this.sinkEnded = true;
|
|
90
|
+
log.trace('%s stream %s sink end - err: %o', this.stat.direction, this.id, err);
|
|
91
|
+
if (err != null && this.endErr == null) {
|
|
92
|
+
this.endErr = err;
|
|
93
|
+
}
|
|
94
|
+
if (this.sourceEnded) {
|
|
95
|
+
this.stat.timeline.close = Date.now();
|
|
96
|
+
if (this.onEnd != null) {
|
|
97
|
+
this.onEnd(this.endErr);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// Close for both Reading and Writing
|
|
102
|
+
close() {
|
|
103
|
+
log.trace('%s stream %s close', this.stat.direction, this.id);
|
|
104
|
+
this.closeRead();
|
|
105
|
+
this.closeWrite();
|
|
106
|
+
}
|
|
107
|
+
// Close for reading
|
|
108
|
+
closeRead() {
|
|
109
|
+
log.trace('%s stream %s closeRead', this.stat.direction, this.id);
|
|
110
|
+
if (this.sourceEnded) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
this.streamSource.end();
|
|
114
|
+
}
|
|
115
|
+
// Close for writing
|
|
116
|
+
closeWrite() {
|
|
117
|
+
log.trace('%s stream %s closeWrite', this.stat.direction, this.id);
|
|
118
|
+
if (this.sinkEnded) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
this.closeController.abort();
|
|
122
|
+
try {
|
|
123
|
+
// need to call this here as the sink method returns in the catch block
|
|
124
|
+
// when the close controller is aborted
|
|
125
|
+
const res = this.sendCloseWrite();
|
|
126
|
+
if (isPromise(res)) {
|
|
127
|
+
res.catch(err => {
|
|
128
|
+
log.error('error while sending close write', err);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
log.trace('%s stream %s error sending close', this.stat.direction, this.id, err);
|
|
134
|
+
}
|
|
135
|
+
this.onSinkEnd();
|
|
136
|
+
}
|
|
137
|
+
// Close for reading and writing (local error)
|
|
138
|
+
abort(err) {
|
|
139
|
+
log.trace('%s stream %s abort', this.stat.direction, this.id, err);
|
|
140
|
+
// End the source with the passed error
|
|
141
|
+
this.streamSource.end(err);
|
|
142
|
+
this.abortController.abort();
|
|
143
|
+
this.onSinkEnd(err);
|
|
144
|
+
}
|
|
145
|
+
// Close immediately for reading and writing (remote error)
|
|
146
|
+
reset() {
|
|
147
|
+
const err = new CodeError('stream reset', ERR_STREAM_RESET);
|
|
148
|
+
this.resetController.abort();
|
|
149
|
+
this.streamSource.end(err);
|
|
150
|
+
this.onSinkEnd(err);
|
|
151
|
+
}
|
|
152
|
+
async sink(source) {
|
|
153
|
+
if (this.sinkSunk) {
|
|
154
|
+
throw new CodeError('sink already called on stream', ERR_DOUBLE_SINK);
|
|
155
|
+
}
|
|
156
|
+
this.sinkSunk = true;
|
|
157
|
+
if (this.sinkEnded) {
|
|
158
|
+
throw new CodeError('stream closed for writing', ERR_SINK_ENDED);
|
|
159
|
+
}
|
|
160
|
+
const signal = anySignal([
|
|
161
|
+
this.abortController.signal,
|
|
162
|
+
this.resetController.signal,
|
|
163
|
+
this.closeController.signal
|
|
164
|
+
]);
|
|
165
|
+
try {
|
|
166
|
+
source = abortableSource(source, signal);
|
|
167
|
+
if (this.stat.direction === 'outbound') { // If initiator, open a new stream
|
|
168
|
+
const res = this.sendNewStream();
|
|
169
|
+
if (isPromise(res)) {
|
|
170
|
+
await res;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
for await (let data of source) {
|
|
174
|
+
while (data.length > 0) {
|
|
175
|
+
if (data.length <= this.maxDataSize) {
|
|
176
|
+
const res = this.sendData(data instanceof Uint8Array ? new Uint8ArrayList(data) : data);
|
|
177
|
+
if (isPromise(res)) { // eslint-disable-line max-depth
|
|
178
|
+
await res;
|
|
179
|
+
}
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
data = data instanceof Uint8Array ? new Uint8ArrayList(data) : data;
|
|
183
|
+
const res = this.sendData(data.sublist(0, this.maxDataSize));
|
|
184
|
+
if (isPromise(res)) {
|
|
185
|
+
await res;
|
|
186
|
+
}
|
|
187
|
+
data.consume(this.maxDataSize);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
catch (err) {
|
|
192
|
+
if (err.type === 'aborted' && err.message === 'The operation was aborted') {
|
|
193
|
+
if (this.closeController.signal.aborted) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (this.resetController.signal.aborted) {
|
|
197
|
+
err.message = 'stream reset';
|
|
198
|
+
err.code = ERR_STREAM_RESET;
|
|
199
|
+
}
|
|
200
|
+
if (this.abortController.signal.aborted) {
|
|
201
|
+
err.message = 'stream aborted';
|
|
202
|
+
err.code = ERR_STREAM_ABORT;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// Send no more data if this stream was remotely reset
|
|
206
|
+
if (err.code === ERR_STREAM_RESET) {
|
|
207
|
+
log.trace('%s stream %s reset', this.stat.direction, this.id);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
log.trace('%s stream %s error', this.stat.direction, this.id, err);
|
|
211
|
+
try {
|
|
212
|
+
const res = this.sendReset();
|
|
213
|
+
if (isPromise(res)) {
|
|
214
|
+
await res;
|
|
215
|
+
}
|
|
216
|
+
this.stat.timeline.reset = Date.now();
|
|
217
|
+
}
|
|
218
|
+
catch (err) {
|
|
219
|
+
log.trace('%s stream %s error sending reset', this.stat.direction, this.id, err);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
this.streamSource.end(err);
|
|
223
|
+
this.onSinkEnd(err);
|
|
224
|
+
throw err;
|
|
225
|
+
}
|
|
226
|
+
finally {
|
|
227
|
+
signal.clear();
|
|
228
|
+
}
|
|
229
|
+
try {
|
|
230
|
+
const res = this.sendCloseWrite();
|
|
231
|
+
if (isPromise(res)) {
|
|
232
|
+
await res;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
catch (err) {
|
|
236
|
+
log.trace('%s stream %s error sending close', this.stat.direction, this.id, err);
|
|
237
|
+
}
|
|
238
|
+
this.onSinkEnd();
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* When an extending class reads data from it's implementation-specific source,
|
|
242
|
+
* call this method to allow the stream consumer to read the data.
|
|
243
|
+
*/
|
|
244
|
+
sourcePush(data) {
|
|
245
|
+
this.streamSource.push(data);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Returns the amount of unread data - can be used to prevent large amounts of
|
|
249
|
+
* data building up when the stream consumer is too slow.
|
|
250
|
+
*/
|
|
251
|
+
sourceReadableLength() {
|
|
252
|
+
return this.streamSource.readableLength;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
//# sourceMappingURL=stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../../src/stream-muxer/stream.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAiB,QAAQ,EAAE,MAAM,aAAa,CAAA;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,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AA;IAClB,CAAC;IAED;;;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"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Connection } from '../connection/index.js';
|
|
2
|
+
import type { PeerId } from '../peer-id/index.js';
|
|
3
|
+
export interface Topology {
|
|
4
|
+
min?: number;
|
|
5
|
+
max?: number;
|
|
6
|
+
onConnect?: (peerId: PeerId, conn: Connection) => void;
|
|
7
|
+
onDisconnect?: (peerId: PeerId) => void;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,KAAK,IAAI,CAAA;IACtD,YAAY,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAA;CACxC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/topology/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { Connection, MultiaddrConnection } from '../connection/index.js';
|
|
2
|
+
import type { EventEmitter } from '../events.js';
|
|
3
|
+
import type { AbortOptions } from '../index.js';
|
|
4
|
+
import type { StreamMuxerFactory } from '../stream-muxer/index.js';
|
|
5
|
+
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
6
|
+
export interface ListenerEvents {
|
|
7
|
+
'connection': CustomEvent<Connection>;
|
|
8
|
+
'listening': CustomEvent;
|
|
9
|
+
'error': CustomEvent<Error>;
|
|
10
|
+
'close': CustomEvent;
|
|
11
|
+
}
|
|
12
|
+
export interface Listener extends EventEmitter<ListenerEvents> {
|
|
13
|
+
/**
|
|
14
|
+
* Start a listener
|
|
15
|
+
*/
|
|
16
|
+
listen: (multiaddr: Multiaddr) => Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Get listen addresses
|
|
19
|
+
*/
|
|
20
|
+
getAddrs: () => Multiaddr[];
|
|
21
|
+
/**
|
|
22
|
+
* Close listener
|
|
23
|
+
*
|
|
24
|
+
* @returns {Promise<void>}
|
|
25
|
+
*/
|
|
26
|
+
close: () => Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
export declare const symbol: unique symbol;
|
|
29
|
+
export interface ConnectionHandler {
|
|
30
|
+
(connection: Connection): void;
|
|
31
|
+
}
|
|
32
|
+
export interface MultiaddrFilter {
|
|
33
|
+
(multiaddrs: Multiaddr[]): Multiaddr[];
|
|
34
|
+
}
|
|
35
|
+
export interface CreateListenerOptions {
|
|
36
|
+
handler?: ConnectionHandler;
|
|
37
|
+
upgrader: Upgrader;
|
|
38
|
+
}
|
|
39
|
+
export interface DialOptions extends AbortOptions {
|
|
40
|
+
upgrader: Upgrader;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* A libp2p transport is understood as something that offers a dial and listen interface to establish connections.
|
|
44
|
+
*/
|
|
45
|
+
export interface Transport {
|
|
46
|
+
/**
|
|
47
|
+
* Used to identify the transport
|
|
48
|
+
*/
|
|
49
|
+
[Symbol.toStringTag]: string;
|
|
50
|
+
/**
|
|
51
|
+
* Used by the isTransport function
|
|
52
|
+
*/
|
|
53
|
+
[symbol]: true;
|
|
54
|
+
/**
|
|
55
|
+
* Dial a given multiaddr.
|
|
56
|
+
*/
|
|
57
|
+
dial: (ma: Multiaddr, options: DialOptions) => Promise<Connection>;
|
|
58
|
+
/**
|
|
59
|
+
* Create transport listeners.
|
|
60
|
+
*/
|
|
61
|
+
createListener: (options: CreateListenerOptions) => Listener;
|
|
62
|
+
/**
|
|
63
|
+
* Takes a list of `Multiaddr`s and returns only valid addresses for the transport
|
|
64
|
+
*/
|
|
65
|
+
filter: MultiaddrFilter;
|
|
66
|
+
}
|
|
67
|
+
export declare function isTransport(other: any): other is Transport;
|
|
68
|
+
/**
|
|
69
|
+
* Enum Transport Manager Fault Tolerance values
|
|
70
|
+
*/
|
|
71
|
+
export declare enum FaultTolerance {
|
|
72
|
+
/**
|
|
73
|
+
* should be used for failing in any listen circumstance
|
|
74
|
+
*/
|
|
75
|
+
FATAL_ALL = 0,
|
|
76
|
+
/**
|
|
77
|
+
* should be used for not failing when not listening
|
|
78
|
+
*/
|
|
79
|
+
NO_FATAL = 1
|
|
80
|
+
}
|
|
81
|
+
export interface UpgraderOptions {
|
|
82
|
+
skipEncryption?: boolean;
|
|
83
|
+
skipProtection?: boolean;
|
|
84
|
+
muxerFactory?: StreamMuxerFactory;
|
|
85
|
+
}
|
|
86
|
+
export interface Upgrader {
|
|
87
|
+
/**
|
|
88
|
+
* Upgrades an outbound connection on `transport.dial`.
|
|
89
|
+
*/
|
|
90
|
+
upgradeOutbound: (maConn: MultiaddrConnection, opts?: UpgraderOptions) => Promise<Connection>;
|
|
91
|
+
/**
|
|
92
|
+
* Upgrades an inbound connection on transport listener.
|
|
93
|
+
*/
|
|
94
|
+
upgradeInbound: (maConn: MultiaddrConnection, opts?: UpgraderOptions) => Promise<Connection>;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/transport/index.ts"],"names":[],"mappings":"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AAC,EAAE,MAAM,CAAA;IAE5B;;OAEG;IACH,CAAC,MAAM,CAAC,EAAE,IAAI,CAAA;IAEd;;OAEG;IACH,IAAI,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,KAAK,OAAO,CAAC,UAAU,CAAC,CAAA;IAElE;;OAEG;IACH,cAAc,EAAE,CAAC,OAAO,EAAE,qBAAqB,KAAK,QAAQ,CAAA;IAE5D;;OAEG;IACH,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,wBAAgB,WAAW,CAAE,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,SAAS,CAE3D;AAED;;GAEG;AACH,oBAAY,cAAc;IACxB;;OAEG;IACH,SAAS,IAAI;IAEb;;OAEG;IACH,QAAQ,IAAA;CACT;AAED,MAAM,WAAW,eAAe;IAC9B,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,YAAY,CAAC,EAAE,kBAAkB,CAAA;CAClC;AAED,MAAM,WAAW,QAAQ;IACvB;;OAEG;IACH,eAAe,EAAE,CAAC,MAAM,EAAE,mBAAmB,EAAE,IAAI,CAAC,EAAE,eAAe,KAAK,OAAO,CAAC,UAAU,CAAC,CAAA;IAE7F;;OAEG;IACH,cAAc,EAAE,CAAC,MAAM,EAAE,mBAAmB,EAAE,IAAI,CAAC,EAAE,eAAe,KAAK,OAAO,CAAC,UAAU,CAAC,CAAA;CAC7F"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const symbol = Symbol.for('@libp2p/transport');
|
|
2
|
+
export function isTransport(other) {
|
|
3
|
+
return other != null && Boolean(other[symbol]);
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Enum Transport Manager Fault Tolerance values
|
|
7
|
+
*/
|
|
8
|
+
export var FaultTolerance;
|
|
9
|
+
(function (FaultTolerance) {
|
|
10
|
+
/**
|
|
11
|
+
* should be used for failing in any listen circumstance
|
|
12
|
+
*/
|
|
13
|
+
FaultTolerance[FaultTolerance["FATAL_ALL"] = 0] = "FATAL_ALL";
|
|
14
|
+
/**
|
|
15
|
+
* should be used for not failing when not listening
|
|
16
|
+
*/
|
|
17
|
+
FaultTolerance[FaultTolerance["NO_FATAL"] = 1] = "NO_FATAL";
|
|
18
|
+
})(FaultTolerance || (FaultTolerance = {}));
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/transport/index.ts"],"names":[],"mappings":"AA8BA,MAAM,CAAC,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAA;AA6CrD,MAAM,UAAU,WAAW,CAAE,KAAU;IACrC,OAAO,KAAK,IAAI,IAAI,IAAI,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAA;AAChD,CAAC;AAED;;GAEG;AACH,MAAM,CAAN,IAAY,cAUX;AAVD,WAAY,cAAc;IACxB;;OAEG;IACH,6DAAa,CAAA;IAEb;;OAEG;IACH,2DAAQ,CAAA;AACV,CAAC,EAVW,cAAc,KAAd,cAAc,QAUzB"}
|