@libp2p/mplex 1.2.1 → 4.0.0
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 +86 -70
- package/dist/src/index.d.ts +13 -7
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +9 -4
- package/dist/src/index.js.map +1 -1
- package/dist/src/mplex.d.ts +8 -3
- package/dist/src/mplex.d.ts.map +1 -1
- package/dist/src/mplex.js +35 -25
- package/dist/src/mplex.js.map +1 -1
- package/dist/src/stream.d.ts.map +1 -1
- package/dist/src/stream.js +6 -2
- package/dist/src/stream.js.map +1 -1
- package/package.json +11 -7
- package/src/index.ts +20 -9
- package/src/mplex.ts +40 -27
- package/src/stream.ts +9 -4
package/README.md
CHANGED
@@ -1,26 +1,41 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
[](http://libp2p.io/)
|
4
|
+
[](http://webchat.freenode.net/?channels=%23libp2p)
|
5
|
+
[](https://discuss.libp2p.io)
|
6
|
+
[](https://codecov.io/gh/libp2p/js-libp2p-mplex)
|
7
|
+
[](https://github.com/libp2p/js-libp2p-mplex/actions/workflows/js-test-and-release.yml)
|
8
|
+
|
9
|
+
> JavaScript implementation of <https://github.com/libp2p/mplex>
|
10
|
+
|
11
|
+
## Table of contents <!-- omit in toc -->
|
12
|
+
|
13
|
+
- [Install](#install)
|
14
|
+
- [Usage](#usage)
|
15
|
+
- [API](#api)
|
16
|
+
- [`const factory = new Mplex([options])`](#const-factory--new-mplexoptions)
|
17
|
+
- [`const muxer = factory.createStreamMuxer(components, [options])`](#const-muxer--factorycreatestreammuxercomponents-options)
|
18
|
+
- [`muxer.onStream`](#muxeronstream)
|
19
|
+
- [`muxer.onStreamEnd`](#muxeronstreamend)
|
20
|
+
- [`muxer.streams`](#muxerstreams)
|
21
|
+
- [`const stream = muxer.newStream([options])`](#const-stream--muxernewstreamoptions)
|
22
|
+
- [`stream.close()`](#streamclose)
|
23
|
+
- [`stream.abort([err])`](#streamaborterr)
|
24
|
+
- [`stream.reset()`](#streamreset)
|
25
|
+
- [`stream.timeline`](#streamtimeline)
|
26
|
+
- [`stream.id`](#streamid)
|
27
|
+
- [Contribute](#contribute)
|
28
|
+
- [License](#license)
|
29
|
+
- [Contribution](#contribution)
|
17
30
|
|
18
31
|
## Install
|
19
32
|
|
20
|
-
```
|
21
|
-
npm
|
33
|
+
```console
|
34
|
+
$ npm i @libp2p/mplex
|
22
35
|
```
|
23
36
|
|
37
|
+
[](https://github.com/libp2p/interface-stream-muxer)
|
38
|
+
|
24
39
|
## Usage
|
25
40
|
|
26
41
|
```js
|
@@ -54,13 +69,14 @@ Creates a factory that can be used to create new muxers.
|
|
54
69
|
|
55
70
|
`options` is an optional `Object` that may have the following properties:
|
56
71
|
|
57
|
-
|
58
|
-
|
59
|
-
|
72
|
+
- `maxMsgSize` - a number that defines how large mplex data messages can be in bytes, if messages are larger than this they will be sent as multiple messages (default: 1048576 - e.g. 1MB)
|
73
|
+
- `maxInboundStreams` - a number that defines how many incoming streams are allowed per connection (default: 1024)
|
74
|
+
- `maxOutboundStreams` - a number that defines how many outgoing streams are allowed per connection (default: 1024)
|
75
|
+
- `maxStreamBufferSize` - a number that defines how large the message buffer is allowed to grow (default: 1024 \* 1024 \* 4 - e.g. 4MB)
|
60
76
|
|
61
77
|
### `const muxer = factory.createStreamMuxer(components, [options])`
|
62
78
|
|
63
|
-
Create a new
|
79
|
+
Create a new *duplex* stream that can be piped together with a connection in order to allow multiplexed communications.
|
64
80
|
|
65
81
|
e.g.
|
66
82
|
|
@@ -77,45 +93,45 @@ pipe(conn, muxer, conn) // conn is duplex connection to another peer
|
|
77
93
|
|
78
94
|
`options` is an optional `Object` that may have the following properties:
|
79
95
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
96
|
+
- `onStream` - A function called when receiving a new stream from the remote. e.g.
|
97
|
+
```js
|
98
|
+
// Receive a new stream on the muxed connection
|
99
|
+
const onStream = stream => {
|
100
|
+
// Read from this stream and write back to it (echo server)
|
101
|
+
pipe(
|
102
|
+
stream,
|
103
|
+
source => (async function * () {
|
104
|
+
for await (const data of source) yield data
|
105
|
+
})(),
|
106
|
+
stream
|
107
|
+
)
|
108
|
+
}
|
109
|
+
const muxer = new Mplex({ onStream })
|
110
|
+
// ...
|
111
|
+
```
|
112
|
+
**Note:** The `onStream` function can be passed in place of the `options` object. i.e.
|
113
|
+
```js
|
114
|
+
new Mplex(stream => { /* ... */ })
|
115
|
+
```
|
116
|
+
- `onStreamEnd` - A function called when a stream ends
|
117
|
+
```js
|
118
|
+
// Receive a notification when a stream ends
|
119
|
+
const onStreamEnd = stream => {
|
120
|
+
// Manage any tracking changes, etc
|
121
|
+
}
|
122
|
+
const muxer = new Mplex({ onStreamEnd })
|
123
|
+
// ...
|
124
|
+
```
|
125
|
+
- `signal` - An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) which can be used to abort the muxer, *including* all of it's multiplexed connections. e.g.
|
126
|
+
```js
|
127
|
+
const controller = new AbortController()
|
128
|
+
const muxer = new Mplex({ signal: controller.signal })
|
129
|
+
|
130
|
+
pipe(conn, muxer, conn)
|
131
|
+
|
132
|
+
controller.abort()
|
133
|
+
```
|
134
|
+
- `maxMsgSize` - The maximum size in bytes the data field of multiplexed messages may contain (default 1MB)
|
119
135
|
|
120
136
|
### `muxer.onStream`
|
121
137
|
|
@@ -143,7 +159,7 @@ const stream = muxer.newStream()
|
|
143
159
|
pipe([1, 2, 3], stream, consume)
|
144
160
|
```
|
145
161
|
|
146
|
-
In addition to `sink` and `source` properties, this stream also has the following API, that will **normally
|
162
|
+
In addition to `sink` and `source` properties, this stream also has the following API, that will **normally *not* be used by stream consumers**.
|
147
163
|
|
148
164
|
#### `stream.close()`
|
149
165
|
|
@@ -155,17 +171,17 @@ The source will return normally, the sink will continue to consume.
|
|
155
171
|
|
156
172
|
#### `stream.abort([err])`
|
157
173
|
|
158
|
-
Closes the stream for **reading**
|
174
|
+
Closes the stream for **reading** *and* **writing**. This should be called when a *local error* has occurred.
|
159
175
|
|
160
176
|
Note, if called without an error any buffered data in the source can still be consumed and the stream will end normally.
|
161
177
|
|
162
|
-
This will cause a `RESET` message to be sent to the remote,
|
178
|
+
This will cause a `RESET` message to be sent to the remote, *unless* the sink has already ended.
|
163
179
|
|
164
180
|
The sink will return and the source will throw if an error is passed or return normally if not.
|
165
181
|
|
166
182
|
#### `stream.reset()`
|
167
183
|
|
168
|
-
Closes the stream
|
184
|
+
Closes the stream *immediately* for **reading** *and* **writing**. This should be called when a *remote error* has occurred.
|
169
185
|
|
170
186
|
This function is called automatically by the muxer when it receives a `RESET` message from the remote.
|
171
187
|
|
@@ -183,16 +199,16 @@ Returns a `string` with an identifier unique to **this** muxer. Identifiers are
|
|
183
199
|
|
184
200
|
The libp2p implementation in JavaScript is a work in progress. As such, there are a few things you can do right now to help out:
|
185
201
|
|
186
|
-
|
187
|
-
|
202
|
+
- Go through the modules and **check out existing issues**. This is especially useful for modules in active development. Some knowledge of IPFS/libp2p may be required, as well as the infrastructure behind it - for instance, you may need to read up on p2p and more complex operations like muxing to be able to help technically.
|
203
|
+
- **Perform code reviews**. More eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs.
|
188
204
|
|
189
205
|
## License
|
190
206
|
|
191
207
|
Licensed under either of
|
192
208
|
|
193
|
-
|
194
|
-
|
209
|
+
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
|
210
|
+
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
|
195
211
|
|
196
|
-
|
212
|
+
## Contribution
|
197
213
|
|
198
214
|
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
|
package/dist/src/index.d.ts
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
import
|
2
|
-
import type { StreamMuxerFactory, StreamMuxerInit } from '@libp2p/
|
3
|
-
import { MplexStreamMuxer } from './mplex.js';
|
1
|
+
import { Components, Initializable } from '@libp2p/components';
|
2
|
+
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface-stream-muxer';
|
4
3
|
export interface MplexInit {
|
5
4
|
/**
|
6
5
|
* The maximum size of message that can be sent in one go in bytes.
|
@@ -12,7 +11,12 @@ export interface MplexInit {
|
|
12
11
|
* The maximum number of multiplexed streams that can be open at any
|
13
12
|
* one time. An attempt to open more than this will throw.
|
14
13
|
*/
|
15
|
-
|
14
|
+
maxInboundStreams?: number;
|
15
|
+
/**
|
16
|
+
* The maximum number of multiplexed streams that can be open at any
|
17
|
+
* one time. An attempt to open more than this will throw.
|
18
|
+
*/
|
19
|
+
maxOutboundStreams?: number;
|
16
20
|
/**
|
17
21
|
* Incoming stream messages are buffered until processed by the stream
|
18
22
|
* handler. If the buffer reaches this size in bytes the stream will
|
@@ -20,10 +24,12 @@ export interface MplexInit {
|
|
20
24
|
*/
|
21
25
|
maxStreamBufferSize?: number;
|
22
26
|
}
|
23
|
-
export declare class Mplex implements StreamMuxerFactory {
|
27
|
+
export declare class Mplex implements StreamMuxerFactory, Initializable {
|
24
28
|
protocol: string;
|
25
|
-
private readonly
|
29
|
+
private readonly _init;
|
30
|
+
private components;
|
26
31
|
constructor(init?: MplexInit);
|
27
|
-
|
32
|
+
init(components: Components): void;
|
33
|
+
createStreamMuxer(init?: StreamMuxerInit): StreamMuxer;
|
28
34
|
}
|
29
35
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAC9D,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAA;AAGtG,MAAM,WAAW,SAAS;IACxB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IAEnB;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAE1B;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAE3B;;;;OAIG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC7B;AAED,qBAAa,KAAM,YAAW,kBAAkB,EAAE,aAAa;IACtD,QAAQ,SAAiB;IAChC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAW;IACjC,OAAO,CAAC,UAAU,CAA+B;gBAEpC,IAAI,GAAE,SAAc;IAIjC,IAAI,CAAE,UAAU,EAAE,UAAU,GAAG,IAAI;IAInC,iBAAiB,CAAE,IAAI,GAAE,eAAoB,GAAG,WAAW;CAM5D"}
|
package/dist/src/index.js
CHANGED
@@ -1,13 +1,18 @@
|
|
1
|
+
import { Components } from '@libp2p/components';
|
1
2
|
import { MplexStreamMuxer } from './mplex.js';
|
2
3
|
export class Mplex {
|
3
4
|
constructor(init = {}) {
|
4
5
|
this.protocol = '/mplex/6.7.0';
|
5
|
-
this.
|
6
|
+
this.components = new Components();
|
7
|
+
this._init = init;
|
6
8
|
}
|
7
|
-
|
8
|
-
|
9
|
+
init(components) {
|
10
|
+
this.components = components;
|
11
|
+
}
|
12
|
+
createStreamMuxer(init = {}) {
|
13
|
+
return new MplexStreamMuxer(this.components, {
|
9
14
|
...init,
|
10
|
-
...this.
|
15
|
+
...this._init
|
11
16
|
});
|
12
17
|
}
|
13
18
|
}
|
package/dist/src/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAiB,MAAM,oBAAoB,CAAA;AAE9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AA8B7C,MAAM,OAAO,KAAK;IAKhB,YAAa,OAAkB,EAAE;QAJ1B,aAAQ,GAAG,cAAc,CAAA;QAExB,eAAU,GAAe,IAAI,UAAU,EAAE,CAAA;QAG/C,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;IACnB,CAAC;IAED,IAAI,CAAE,UAAsB;QAC1B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;IAC9B,CAAC;IAED,iBAAiB,CAAE,OAAwB,EAAE;QAC3C,OAAO,IAAI,gBAAgB,CAAC,IAAI,CAAC,UAAU,EAAE;YAC3C,GAAG,IAAI;YACP,GAAG,IAAI,CAAC,KAAK;SACd,CAAC,CAAA;IACJ,CAAC;CACF"}
|
package/dist/src/mplex.d.ts
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
import { Pushable } from 'it-pushable';
|
2
2
|
import { Message } from './message-types.js';
|
3
|
-
import type { Components } from '@libp2p/
|
3
|
+
import type { Components } from '@libp2p/components';
|
4
4
|
import type { Sink } from 'it-stream-types';
|
5
|
-
import type { StreamMuxer, StreamMuxerInit } from '@libp2p/
|
6
|
-
import type { Stream } from '@libp2p/
|
5
|
+
import type { StreamMuxer, StreamMuxerInit } from '@libp2p/interface-stream-muxer';
|
6
|
+
import type { Stream } from '@libp2p/interface-connection';
|
7
7
|
import type { MplexInit } from './index.js';
|
8
8
|
export interface MplexStream extends Stream {
|
9
9
|
source: Pushable<Uint8Array>;
|
@@ -18,6 +18,7 @@ export declare class MplexStreamMuxer implements StreamMuxer {
|
|
18
18
|
private readonly _streams;
|
19
19
|
private readonly _init;
|
20
20
|
private readonly _source;
|
21
|
+
private readonly closeController;
|
21
22
|
constructor(components: Components, init?: MplexStreamMuxerInit);
|
22
23
|
init(components: Components): void;
|
23
24
|
/**
|
@@ -29,6 +30,10 @@ export declare class MplexStreamMuxer implements StreamMuxer {
|
|
29
30
|
* provided, the id of the stream will be used.
|
30
31
|
*/
|
31
32
|
newStream(name?: string): Stream;
|
33
|
+
/**
|
34
|
+
* Close or abort all tracked streams and stop the muxer
|
35
|
+
*/
|
36
|
+
close(err?: Error | undefined): void;
|
32
37
|
/**
|
33
38
|
* Called whenever an inbound stream is created
|
34
39
|
*/
|
package/dist/src/mplex.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"mplex.d.ts","sourceRoot":"","sources":["../../src/mplex.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAa,MAAM,aAAa,CAAA;AAKjD,OAAO,EAAkC,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAM5E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM
|
1
|
+
{"version":3,"file":"mplex.d.ts","sourceRoot":"","sources":["../../src/mplex.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAa,MAAM,aAAa,CAAA;AAKjD,OAAO,EAAkC,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAM5E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAA;AAClF,OAAO,KAAK,EAAE,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GG;IACH,aAAa;;;;;;;IAgBb,eAAe,CAAE,OAAO,EAAE,OAAO;CA+DlC"}
|
package/dist/src/mplex.js
CHANGED
@@ -10,8 +10,10 @@ import { toString as uint8ArrayToString } from 'uint8arrays';
|
|
10
10
|
import { trackedMap } from '@libp2p/tracked-map';
|
11
11
|
import { logger } from '@libp2p/logger';
|
12
12
|
import errCode from 'err-code';
|
13
|
+
import anySignal from 'any-signal';
|
13
14
|
const log = logger('libp2p:mplex');
|
14
|
-
const
|
15
|
+
const MAX_STREAMS_INBOUND_STREAMS_PER_CONNECTION = 1024;
|
16
|
+
const MAX_STREAMS_OUTBOUND_STREAMS_PER_CONNECTION = 1024;
|
15
17
|
const MAX_STREAM_BUFFER_SIZE = 1024 * 1024 * 4; // 4MB
|
16
18
|
function printMessage(msg) {
|
17
19
|
const output = {
|
@@ -52,9 +54,12 @@ export class MplexStreamMuxer {
|
|
52
54
|
const source = this._createSource();
|
53
55
|
this._source = source;
|
54
56
|
this.source = source;
|
57
|
+
/**
|
58
|
+
* Close controller
|
59
|
+
*/
|
60
|
+
this.closeController = new AbortController();
|
55
61
|
}
|
56
|
-
init(components) {
|
57
|
-
}
|
62
|
+
init(components) { }
|
58
63
|
/**
|
59
64
|
* Returns a Map of streams and their ids
|
60
65
|
*/
|
@@ -74,11 +79,28 @@ export class MplexStreamMuxer {
|
|
74
79
|
* provided, the id of the stream will be used.
|
75
80
|
*/
|
76
81
|
newStream(name) {
|
82
|
+
if (this.closeController.signal.aborted) {
|
83
|
+
throw new Error('Muxer already closed');
|
84
|
+
}
|
77
85
|
const id = this._streamId++;
|
78
86
|
name = name == null ? id.toString() : name.toString();
|
79
87
|
const registry = this._streams.initiators;
|
80
88
|
return this._newStream({ id, name, type: 'initiator', registry });
|
81
89
|
}
|
90
|
+
/**
|
91
|
+
* Close or abort all tracked streams and stop the muxer
|
92
|
+
*/
|
93
|
+
close(err) {
|
94
|
+
if (this.closeController.signal.aborted)
|
95
|
+
return;
|
96
|
+
if (err != null) {
|
97
|
+
this.streams.forEach(s => s.abort(err));
|
98
|
+
}
|
99
|
+
else {
|
100
|
+
this.streams.forEach(s => s.close());
|
101
|
+
}
|
102
|
+
this.closeController.abort();
|
103
|
+
}
|
82
104
|
/**
|
83
105
|
* Called whenever an inbound stream is created
|
84
106
|
*/
|
@@ -88,9 +110,11 @@ export class MplexStreamMuxer {
|
|
88
110
|
return this._newStream({ id, name, type: 'receiver', registry });
|
89
111
|
}
|
90
112
|
_newStream(options) {
|
91
|
-
|
92
|
-
|
93
|
-
|
113
|
+
if (this._streams.initiators.size === (this._init.maxOutboundStreams ?? MAX_STREAMS_OUTBOUND_STREAMS_PER_CONNECTION)) {
|
114
|
+
throw errCode(new Error('To many outgoing streams open'), 'ERR_TOO_MANY_OUTBOUND_STREAMS');
|
115
|
+
}
|
116
|
+
if (this._streams.receivers.size === (this._init.maxInboundStreams ?? MAX_STREAMS_INBOUND_STREAMS_PER_CONNECTION)) {
|
117
|
+
throw errCode(new Error('To many incoming streams open'), 'ERR_TOO_MANY_INBOUND_STREAMS');
|
94
118
|
}
|
95
119
|
const { id, name, type, registry } = options;
|
96
120
|
log('new %s stream %s %s', type, id, name);
|
@@ -126,9 +150,12 @@ export class MplexStreamMuxer {
|
|
126
150
|
*/
|
127
151
|
_createSink() {
|
128
152
|
const sink = async (source) => {
|
153
|
+
// see: https://github.com/jacobheun/any-signal/pull/18
|
154
|
+
const abortSignals = [this.closeController.signal];
|
129
155
|
if (this._init.signal != null) {
|
130
|
-
|
156
|
+
abortSignals.push(this._init.signal);
|
131
157
|
}
|
158
|
+
source = abortableSource(source, anySignal(abortSignals));
|
132
159
|
try {
|
133
160
|
await pipe(source, decode, restrictSize(this._init.maxMsgSize), async (source) => {
|
134
161
|
for await (const msg of source) {
|
@@ -150,24 +177,7 @@ export class MplexStreamMuxer {
|
|
150
177
|
*/
|
151
178
|
_createSource() {
|
152
179
|
const onEnd = (err) => {
|
153
|
-
|
154
|
-
// Abort all the things!
|
155
|
-
for (const s of initiators.values()) {
|
156
|
-
if (err != null) {
|
157
|
-
s.abort(err);
|
158
|
-
}
|
159
|
-
else {
|
160
|
-
s.close();
|
161
|
-
}
|
162
|
-
}
|
163
|
-
for (const s of receivers.values()) {
|
164
|
-
if (err != null) {
|
165
|
-
s.abort(err);
|
166
|
-
}
|
167
|
-
else {
|
168
|
-
s.close();
|
169
|
-
}
|
170
|
-
}
|
180
|
+
this.close(err);
|
171
181
|
};
|
172
182
|
const source = pushableV({
|
173
183
|
objectMode: true,
|
package/dist/src/mplex.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"mplex.js","sourceRoot":"","sources":["../../src/mplex.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAY,SAAS,EAAE,MAAM,aAAa,CAAA;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACjD,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAW,MAAM,oBAAoB,CAAA;AAC5E,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,OAAO,MAAM,UAAU,CAAA;
|
1
|
+
{"version":3,"file":"mplex.js","sourceRoot":"","sources":["../../src/mplex.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAY,SAAS,EAAE,MAAM,aAAa,CAAA;AACjD,OAAO,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MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,IAAI,CAAA;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,CAAA;IAC7B,IAAI,CAAC,EAAE,WAAW,GAAG,UAAU,CAAA;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,wBAAgB,YAAY,CAAE,OAAO,EAAE,OAAO,GAAG,WAAW,
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAGjD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAQ7C,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,IAAI,CAAA;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,CAAA;IAC7B,IAAI,CAAC,EAAE,WAAW,GAAG,UAAU,CAAA;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,wBAAgB,YAAY,CAAE,OAAO,EAAE,OAAO,GAAG,WAAW,CA+M3D"}
|
package/dist/src/stream.js
CHANGED
@@ -35,7 +35,7 @@ export function createStream(options) {
|
|
35
35
|
endErr = err;
|
36
36
|
}
|
37
37
|
if (sinkEnded) {
|
38
|
-
stream.timeline.close = Date.now();
|
38
|
+
stream.stat.timeline.close = Date.now();
|
39
39
|
if (onEnd != null) {
|
40
40
|
onEnd(endErr);
|
41
41
|
}
|
@@ -172,7 +172,11 @@ export function createStream(options) {
|
|
172
172
|
source: pushable({
|
173
173
|
onEnd: onSourceEnd
|
174
174
|
}),
|
175
|
-
|
175
|
+
stat: {
|
176
|
+
direction: type === 'initiator' ? 'outbound' : 'inbound',
|
177
|
+
timeline
|
178
|
+
},
|
179
|
+
metadata: {},
|
176
180
|
id: externalId
|
177
181
|
};
|
178
182
|
return stream;
|
package/dist/src/stream.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,OAAO,MAAM,UAAU,CAAA;AAC9B,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AAChF,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAMvC,MAAM,GAAG,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAA;AAEzC,MAAM,sBAAsB,GAAG,wBAAwB,CAAA;AACvD,MAAM,sBAAsB,GAAG,wBAAwB,CAAA;AACvD,MAAM,oBAAoB,GAAG,sBAAsB,CAAA;AAWnD,MAAM,UAAU,YAAY,CAAE,OAAgB;IAC5C,MAAM,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,GAAG,WAAW,EAAE,UAAU,GAAG,YAAY,EAAE,GAAG,OAAO,CAAA;IAExF,MAAM,eAAe,GAAG,IAAI,eAAe,EAAE,CAAA;IAC7C,MAAM,eAAe,GAAG,IAAI,eAAe,EAAE,CAAA;IAC7C,MAAM,eAAe,GA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CAAC"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@libp2p/mplex",
|
3
|
-
"version": "
|
3
|
+
"version": "4.0.0",
|
4
4
|
"description": "JavaScript implementation of https://github.com/libp2p/mplex",
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p-mplex#readme",
|
@@ -35,6 +35,7 @@
|
|
35
35
|
],
|
36
36
|
"exports": {
|
37
37
|
".": {
|
38
|
+
"types": "./dist/src/index.d.ts",
|
38
39
|
"import": "./dist/src/index.js"
|
39
40
|
}
|
40
41
|
},
|
@@ -140,8 +141,11 @@
|
|
140
141
|
"release": "aegir release"
|
141
142
|
},
|
142
143
|
"dependencies": {
|
143
|
-
"@libp2p/
|
144
|
-
"@libp2p/
|
144
|
+
"@libp2p/components": "^2.0.0",
|
145
|
+
"@libp2p/interface-connection": "^2.0.0",
|
146
|
+
"@libp2p/interface-stream-muxer": "^2.0.0",
|
147
|
+
"@libp2p/logger": "^2.0.0",
|
148
|
+
"@libp2p/tracked-map": "^2.0.0",
|
145
149
|
"abortable-iterator": "^4.0.2",
|
146
150
|
"any-signal": "^3.0.0",
|
147
151
|
"err-code": "^3.0.1",
|
@@ -153,10 +157,9 @@
|
|
153
157
|
"varint": "^6.0.0"
|
154
158
|
},
|
155
159
|
"devDependencies": {
|
156
|
-
"@libp2p/interface-compliance-tests": "^
|
157
|
-
"@libp2p/interfaces": "^2.0.1",
|
160
|
+
"@libp2p/interface-stream-muxer-compliance-tests": "^3.0.1",
|
158
161
|
"@types/varint": "^6.0.0",
|
159
|
-
"aegir": "^37.0
|
162
|
+
"aegir": "^37.2.0",
|
160
163
|
"cborg": "^1.8.1",
|
161
164
|
"delay": "^5.0.0",
|
162
165
|
"iso-random-stream": "^2.0.2",
|
@@ -165,6 +168,7 @@
|
|
165
168
|
"it-foreach": "^0.1.1",
|
166
169
|
"it-map": "^1.0.6",
|
167
170
|
"p-defer": "^4.0.0",
|
168
|
-
"random-int": "^3.0.0"
|
171
|
+
"random-int": "^3.0.0",
|
172
|
+
"typescript": "^4.7.4"
|
169
173
|
}
|
170
174
|
}
|
package/src/index.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import
|
2
|
-
import type { StreamMuxerFactory, StreamMuxerInit } from '@libp2p/
|
1
|
+
import { Components, Initializable } from '@libp2p/components'
|
2
|
+
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface-stream-muxer'
|
3
3
|
import { MplexStreamMuxer } from './mplex.js'
|
4
4
|
|
5
5
|
export interface MplexInit {
|
@@ -14,7 +14,13 @@ export interface MplexInit {
|
|
14
14
|
* The maximum number of multiplexed streams that can be open at any
|
15
15
|
* one time. An attempt to open more than this will throw.
|
16
16
|
*/
|
17
|
-
|
17
|
+
maxInboundStreams?: number
|
18
|
+
|
19
|
+
/**
|
20
|
+
* The maximum number of multiplexed streams that can be open at any
|
21
|
+
* one time. An attempt to open more than this will throw.
|
22
|
+
*/
|
23
|
+
maxOutboundStreams?: number
|
18
24
|
|
19
25
|
/**
|
20
26
|
* Incoming stream messages are buffered until processed by the stream
|
@@ -24,18 +30,23 @@ export interface MplexInit {
|
|
24
30
|
maxStreamBufferSize?: number
|
25
31
|
}
|
26
32
|
|
27
|
-
export class Mplex implements StreamMuxerFactory {
|
33
|
+
export class Mplex implements StreamMuxerFactory, Initializable {
|
28
34
|
public protocol = '/mplex/6.7.0'
|
29
|
-
private readonly
|
35
|
+
private readonly _init: MplexInit
|
36
|
+
private components: Components = new Components()
|
30
37
|
|
31
38
|
constructor (init: MplexInit = {}) {
|
32
|
-
this.
|
39
|
+
this._init = init
|
40
|
+
}
|
41
|
+
|
42
|
+
init (components: Components): void {
|
43
|
+
this.components = components
|
33
44
|
}
|
34
45
|
|
35
|
-
createStreamMuxer (
|
36
|
-
return new MplexStreamMuxer(components, {
|
46
|
+
createStreamMuxer (init: StreamMuxerInit = {}): StreamMuxer {
|
47
|
+
return new MplexStreamMuxer(this.components, {
|
37
48
|
...init,
|
38
|
-
...this.
|
49
|
+
...this._init
|
39
50
|
})
|
40
51
|
}
|
41
52
|
}
|
package/src/mplex.ts
CHANGED
@@ -10,15 +10,17 @@ import { toString as uint8ArrayToString } from 'uint8arrays'
|
|
10
10
|
import { trackedMap } from '@libp2p/tracked-map'
|
11
11
|
import { logger } from '@libp2p/logger'
|
12
12
|
import errCode from 'err-code'
|
13
|
-
import type { Components } from '@libp2p/
|
13
|
+
import type { Components } from '@libp2p/components'
|
14
14
|
import type { Sink } from 'it-stream-types'
|
15
|
-
import type { StreamMuxer, StreamMuxerInit } from '@libp2p/
|
16
|
-
import type { Stream } from '@libp2p/
|
15
|
+
import type { StreamMuxer, StreamMuxerInit } from '@libp2p/interface-stream-muxer'
|
16
|
+
import type { Stream } from '@libp2p/interface-connection'
|
17
17
|
import type { MplexInit } from './index.js'
|
18
|
+
import anySignal from 'any-signal'
|
18
19
|
|
19
20
|
const log = logger('libp2p:mplex')
|
20
21
|
|
21
|
-
const
|
22
|
+
const MAX_STREAMS_INBOUND_STREAMS_PER_CONNECTION = 1024
|
23
|
+
const MAX_STREAMS_OUTBOUND_STREAMS_PER_CONNECTION = 1024
|
22
24
|
const MAX_STREAM_BUFFER_SIZE = 1024 * 1024 * 4 // 4MB
|
23
25
|
|
24
26
|
function printMessage (msg: Message) {
|
@@ -54,6 +56,7 @@ export class MplexStreamMuxer implements StreamMuxer {
|
|
54
56
|
private readonly _streams: { initiators: Map<number, MplexStream>, receivers: Map<number, MplexStream> }
|
55
57
|
private readonly _init: MplexStreamMuxerInit
|
56
58
|
private readonly _source: { push: (val: Message) => void, end: (err?: Error) => void }
|
59
|
+
private readonly closeController: AbortController
|
57
60
|
|
58
61
|
constructor (components: Components, init?: MplexStreamMuxerInit) {
|
59
62
|
init = init ?? {}
|
@@ -82,12 +85,15 @@ export class MplexStreamMuxer implements StreamMuxer {
|
|
82
85
|
const source = this._createSource()
|
83
86
|
this._source = source
|
84
87
|
this.source = source
|
85
|
-
}
|
86
|
-
|
87
|
-
init (components: Components) {
|
88
88
|
|
89
|
+
/**
|
90
|
+
* Close controller
|
91
|
+
*/
|
92
|
+
this.closeController = new AbortController()
|
89
93
|
}
|
90
94
|
|
95
|
+
init (components: Components) {}
|
96
|
+
|
91
97
|
/**
|
92
98
|
* Returns a Map of streams and their ids
|
93
99
|
*/
|
@@ -108,12 +114,29 @@ export class MplexStreamMuxer implements StreamMuxer {
|
|
108
114
|
* provided, the id of the stream will be used.
|
109
115
|
*/
|
110
116
|
newStream (name?: string): Stream {
|
117
|
+
if (this.closeController.signal.aborted) {
|
118
|
+
throw new Error('Muxer already closed')
|
119
|
+
}
|
111
120
|
const id = this._streamId++
|
112
121
|
name = name == null ? id.toString() : name.toString()
|
113
122
|
const registry = this._streams.initiators
|
114
123
|
return this._newStream({ id, name, type: 'initiator', registry })
|
115
124
|
}
|
116
125
|
|
126
|
+
/**
|
127
|
+
* Close or abort all tracked streams and stop the muxer
|
128
|
+
*/
|
129
|
+
close (err?: Error | undefined): void {
|
130
|
+
if (this.closeController.signal.aborted) return
|
131
|
+
|
132
|
+
if (err != null) {
|
133
|
+
this.streams.forEach(s => s.abort(err))
|
134
|
+
} else {
|
135
|
+
this.streams.forEach(s => s.close())
|
136
|
+
}
|
137
|
+
this.closeController.abort()
|
138
|
+
}
|
139
|
+
|
117
140
|
/**
|
118
141
|
* Called whenever an inbound stream is created
|
119
142
|
*/
|
@@ -124,10 +147,12 @@ export class MplexStreamMuxer implements StreamMuxer {
|
|
124
147
|
}
|
125
148
|
|
126
149
|
_newStream (options: { id: number, name: string, type: 'initiator' | 'receiver', registry: Map<number, MplexStream> }) {
|
127
|
-
|
150
|
+
if (this._streams.initiators.size === (this._init.maxOutboundStreams ?? MAX_STREAMS_OUTBOUND_STREAMS_PER_CONNECTION)) {
|
151
|
+
throw errCode(new Error('To many outgoing streams open'), 'ERR_TOO_MANY_OUTBOUND_STREAMS')
|
152
|
+
}
|
128
153
|
|
129
|
-
if (
|
130
|
-
throw errCode(new Error('
|
154
|
+
if (this._streams.receivers.size === (this._init.maxInboundStreams ?? MAX_STREAMS_INBOUND_STREAMS_PER_CONNECTION)) {
|
155
|
+
throw errCode(new Error('To many incoming streams open'), 'ERR_TOO_MANY_INBOUND_STREAMS')
|
131
156
|
}
|
132
157
|
|
133
158
|
const { id, name, type, registry } = options
|
@@ -174,9 +199,12 @@ export class MplexStreamMuxer implements StreamMuxer {
|
|
174
199
|
*/
|
175
200
|
_createSink () {
|
176
201
|
const sink: Sink<Uint8Array> = async source => {
|
202
|
+
// see: https://github.com/jacobheun/any-signal/pull/18
|
203
|
+
const abortSignals = [this.closeController.signal]
|
177
204
|
if (this._init.signal != null) {
|
178
|
-
|
205
|
+
abortSignals.push(this._init.signal)
|
179
206
|
}
|
207
|
+
source = abortableSource(source, anySignal(abortSignals))
|
180
208
|
|
181
209
|
try {
|
182
210
|
await pipe(
|
@@ -206,22 +234,7 @@ export class MplexStreamMuxer implements StreamMuxer {
|
|
206
234
|
*/
|
207
235
|
_createSource () {
|
208
236
|
const onEnd = (err?: Error) => {
|
209
|
-
|
210
|
-
// Abort all the things!
|
211
|
-
for (const s of initiators.values()) {
|
212
|
-
if (err != null) {
|
213
|
-
s.abort(err)
|
214
|
-
} else {
|
215
|
-
s.close()
|
216
|
-
}
|
217
|
-
}
|
218
|
-
for (const s of receivers.values()) {
|
219
|
-
if (err != null) {
|
220
|
-
s.abort(err)
|
221
|
-
} else {
|
222
|
-
s.close()
|
223
|
-
}
|
224
|
-
}
|
237
|
+
this.close(err)
|
225
238
|
}
|
226
239
|
const source = pushableV<Message>({
|
227
240
|
objectMode: true,
|
package/src/stream.ts
CHANGED
@@ -8,7 +8,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
8
8
|
import { Uint8ArrayList } from 'uint8arraylist'
|
9
9
|
import { logger } from '@libp2p/logger'
|
10
10
|
import type { Message } from './message-types.js'
|
11
|
-
import type {
|
11
|
+
import type { StreamTimeline } from '@libp2p/interface-connection'
|
12
12
|
import type { Source } from 'it-stream-types'
|
13
13
|
import type { MplexStream } from './mplex.js'
|
14
14
|
|
@@ -41,7 +41,7 @@ export function createStream (options: Options): MplexStream {
|
|
41
41
|
let sinkEnded = false
|
42
42
|
let endErr: Error | undefined
|
43
43
|
|
44
|
-
const timeline:
|
44
|
+
const timeline: StreamTimeline = {
|
45
45
|
open: Date.now()
|
46
46
|
}
|
47
47
|
|
@@ -58,7 +58,7 @@ export function createStream (options: Options): MplexStream {
|
|
58
58
|
}
|
59
59
|
|
60
60
|
if (sinkEnded) {
|
61
|
-
stream.timeline.close = Date.now()
|
61
|
+
stream.stat.timeline.close = Date.now()
|
62
62
|
|
63
63
|
if (onEnd != null) {
|
64
64
|
onEnd(endErr)
|
@@ -223,7 +223,12 @@ export function createStream (options: Options): MplexStream {
|
|
223
223
|
onEnd: onSourceEnd
|
224
224
|
}),
|
225
225
|
|
226
|
-
|
226
|
+
stat: {
|
227
|
+
direction: type === 'initiator' ? 'outbound' : 'inbound',
|
228
|
+
timeline
|
229
|
+
},
|
230
|
+
|
231
|
+
metadata: {},
|
227
232
|
|
228
233
|
id: externalId
|
229
234
|
}
|