@libp2p/mplex 1.0.0 → 1.0.3

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,66 +1,11 @@
1
- import { Pushable } from 'it-pushable';
2
- import { Message } from './message-types.js';
3
- import type { AbortOptions } from '@libp2p/interfaces';
4
- import type { Sink } from 'it-stream-types';
5
- import type { Muxer } from '@libp2p/interfaces/stream-muxer';
6
- import type { Stream } from '@libp2p/interfaces/connection';
7
- import type { ComponentMetricsTracker } from '@libp2p/interfaces/metrics';
8
- export interface MplexStream extends Stream {
9
- source: Pushable<Uint8Array>;
10
- }
11
- export interface MplexOptions extends AbortOptions {
12
- onStream?: (...args: any[]) => void;
13
- onStreamEnd?: (...args: any[]) => void;
1
+ import type { Components } from '@libp2p/interfaces/components';
2
+ import type { StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interfaces/stream-muxer';
3
+ import { MplexStreamMuxer } from './mplex.js';
4
+ export interface MplexInit extends StreamMuxerInit {
14
5
  maxMsgSize?: number;
15
- metrics?: ComponentMetricsTracker;
16
6
  }
17
- export declare class Mplex implements Muxer {
18
- static multicodec: string;
19
- sink: Sink<Uint8Array>;
20
- source: AsyncIterable<Uint8Array>;
21
- private _streamId;
22
- private readonly _streams;
23
- private readonly _options;
24
- private readonly _source;
25
- constructor(options?: MplexOptions);
26
- /**
27
- * Returns a Map of streams and their ids
28
- */
29
- get streams(): Stream[];
30
- /**
31
- * Initiate a new stream with the given name. If no name is
32
- * provided, the id of the stream will be used.
33
- */
34
- newStream(name?: string): Stream;
35
- /**
36
- * Called whenever an inbound stream is created
37
- */
38
- _newReceiverStream(options: {
39
- id: number;
40
- name: string;
41
- }): MplexStream;
42
- _newStream(options: {
43
- id: number;
44
- name: string;
45
- type: 'initiator' | 'receiver';
46
- registry: Map<number, MplexStream>;
47
- }): MplexStream;
48
- /**
49
- * Creates a sink with an abortable source. Incoming messages will
50
- * also have their size restricted. All messages will be varint decoded.
51
- */
52
- _createSink(): Sink<Uint8Array, Promise<void>>;
53
- /**
54
- * Creates a source that restricts outgoing message sizes
55
- * and varint encodes them
56
- */
57
- _createSource(): AsyncGenerator<Uint8Array, void, undefined> & {
58
- push: (value: Message) => import("it-pushable").PushableV<Message>;
59
- end: (err?: Error | undefined) => import("it-pushable").PushableV<Message>;
60
- return: () => {
61
- done: boolean;
62
- };
63
- };
64
- _handleIncoming(message: Message): void;
7
+ export declare class Mplex implements StreamMuxerFactory {
8
+ protocol: string;
9
+ createStreamMuxer(components: Components, init?: MplexInit): MplexStreamMuxer;
65
10
  }
66
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAa,MAAM,aAAa,CAAA;AAKjD,OAAO,EAAkC,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAK5E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iCAAiC,CAAA;AAC5D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,+BAA+B,CAAA;AAC3D,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAA;AAsBzE,MAAM,WAAW,WAAY,SAAQ,MAAM;IACzC,MAAM,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAA;CAC7B;AAED,MAAM,WAAW,YAAa,SAAQ,YAAY;IAChD,QAAQ,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAA;IACnC,WAAW,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAA;IACtC,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,OAAO,CAAC,EAAE,uBAAuB,CAAA;C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M,YAAW,kBAAkB;IACvC,QAAQ,SAAiB;IAEhC,iBAAiB,CAAE,UAAU,EAAE,UAAU,EAAE,IAAI,CAAC,EAAE,SAAS;CAG5D"}
package/dist/src/index.js CHANGED
@@ -1,218 +1,10 @@
1
- import { pipe } from 'it-pipe';
2
- import { pushableV } from 'it-pushable';
3
- import { abortableSource } from 'abortable-iterator';
4
- import { encode } from './encode.js';
5
- import { decode } from './decode.js';
6
- import { restrictSize } from './restrict-size.js';
7
- import { MessageTypes, MessageTypeNames } from './message-types.js';
8
- import { createStream } from './stream.js';
9
- import { toString as uint8ArrayToString } from 'uint8arrays';
10
- import { trackedMap } from '@libp2p/tracked-map';
11
- import { logger } from '@libp2p/logger';
12
- import each from 'it-foreach';
13
- const log = logger('libp2p:mplex');
14
- function printMessage(msg) {
15
- const output = {
16
- ...msg,
17
- type: `${MessageTypeNames[msg.type]} (${msg.type})`
18
- };
19
- if (msg.type === MessageTypes.NEW_STREAM) {
20
- output.data = uint8ArrayToString(msg.data instanceof Uint8Array ? msg.data : msg.data.slice());
21
- }
22
- if (msg.type === MessageTypes.MESSAGE_INITIATOR || msg.type === MessageTypes.MESSAGE_RECEIVER) {
23
- output.data = uint8ArrayToString(msg.data instanceof Uint8Array ? msg.data : msg.data.slice(), 'base16');
24
- }
25
- return output;
26
- }
1
+ import { MplexStreamMuxer } from './mplex.js';
27
2
  export class Mplex {
28
- constructor(options) {
29
- options = options ?? {};
30
- this._streamId = 0;
31
- this._streams = {
32
- /**
33
- * Stream to ids map
34
- */
35
- initiators: trackedMap({ metrics: options.metrics, component: 'mplex', metric: 'initiatorStreams' }),
36
- /**
37
- * Stream to ids map
38
- */
39
- receivers: trackedMap({ metrics: options.metrics, component: 'mplex', metric: 'receiverStreams' })
40
- };
41
- this._options = options;
42
- /**
43
- * An iterable sink
44
- */
45
- this.sink = this._createSink();
46
- /**
47
- * An iterable source
48
- */
49
- const source = this._createSource();
50
- this._source = source;
51
- this.source = source;
52
- }
53
- /**
54
- * Returns a Map of streams and their ids
55
- */
56
- get streams() {
57
- // Inbound and Outbound streams may have the same ids, so we need to make those unique
58
- const streams = [];
59
- this._streams.initiators.forEach(stream => {
60
- streams.push(stream);
61
- });
62
- this._streams.receivers.forEach(stream => {
63
- streams.push(stream);
64
- });
65
- return streams;
66
- }
67
- /**
68
- * Initiate a new stream with the given name. If no name is
69
- * provided, the id of the stream will be used.
70
- */
71
- newStream(name) {
72
- const id = this._streamId++;
73
- name = name == null ? id.toString() : name.toString();
74
- const registry = this._streams.initiators;
75
- return this._newStream({ id, name, type: 'initiator', registry });
76
- }
77
- /**
78
- * Called whenever an inbound stream is created
79
- */
80
- _newReceiverStream(options) {
81
- const { id, name } = options;
82
- const registry = this._streams.receivers;
83
- return this._newStream({ id, name, type: 'receiver', registry });
84
- }
85
- _newStream(options) {
86
- const { id, name, type, registry } = options;
87
- log('new %s stream %s %s', type, id, name);
88
- if (registry.has(id)) {
89
- throw new Error(`${type} stream ${id} already exists!`);
90
- }
91
- const send = (msg) => {
92
- if (log.enabled) {
93
- log('%s stream %s send', type, id, printMessage(msg));
94
- }
95
- if (msg.type === MessageTypes.NEW_STREAM || msg.type === MessageTypes.MESSAGE_INITIATOR || msg.type === MessageTypes.MESSAGE_RECEIVER) {
96
- msg.data = msg.data instanceof Uint8Array ? msg.data : msg.data.slice();
97
- }
98
- this._source.push(msg);
99
- };
100
- const onEnd = () => {
101
- log('%s stream %s %s ended', type, id, name);
102
- registry.delete(id);
103
- if (this._options.onStreamEnd != null) {
104
- this._options.onStreamEnd(stream);
105
- }
106
- };
107
- const stream = createStream({ id, name, send, type, onEnd, maxMsgSize: this._options.maxMsgSize });
108
- registry.set(id, stream);
109
- return stream;
110
- }
111
- /**
112
- * Creates a sink with an abortable source. Incoming messages will
113
- * also have their size restricted. All messages will be varint decoded.
114
- */
115
- _createSink() {
116
- const sink = async (source) => {
117
- if (this._options.signal != null) {
118
- source = abortableSource(source, this._options.signal);
119
- }
120
- try {
121
- await pipe(source, source => each(source, (buf) => {
122
- // console.info('incoming', uint8ArrayToString(buf, 'base64'))
123
- }), decode, restrictSize(this._options.maxMsgSize), async (source) => {
124
- for await (const msg of source) {
125
- this._handleIncoming(msg);
126
- }
127
- });
128
- this._source.end();
129
- }
130
- catch (err) {
131
- log('error in sink', err);
132
- this._source.end(err); // End the source with an error
133
- }
134
- };
135
- return sink;
136
- }
137
- /**
138
- * Creates a source that restricts outgoing message sizes
139
- * and varint encodes them
140
- */
141
- _createSource() {
142
- const onEnd = (err) => {
143
- const { initiators, receivers } = this._streams;
144
- // Abort all the things!
145
- for (const s of initiators.values()) {
146
- s.abort(err);
147
- }
148
- for (const s of receivers.values()) {
149
- s.abort(err);
150
- }
151
- };
152
- const source = pushableV({ onEnd });
153
- /*
154
- const p = pipe(
155
- source,
156
- source => each(source, (msgs) => {
157
- if (log.enabled) {
158
- msgs.forEach(msg => {
159
- log('outgoing message', printMessage(msg))
160
- })
161
- }
162
- }),
163
- source => encode(source),
164
- source => each(source, (buf) => {
165
- console.info('outgoing', uint8ArrayToString(buf, 'base64'))
166
- })
167
- )
168
-
169
- return Object.assign(p, {
170
- push: source.push,
171
- end: source.end,
172
- return: source.return
173
- })
174
- */
175
- return Object.assign(encode(source), {
176
- push: source.push,
177
- end: source.end,
178
- return: source.return
179
- });
3
+ constructor() {
4
+ this.protocol = '/mplex/6.7.0';
180
5
  }
181
- _handleIncoming(message) {
182
- const { id, type } = message;
183
- if (log.enabled) {
184
- log('incoming message', printMessage(message));
185
- }
186
- // Create a new stream?
187
- if (message.type === MessageTypes.NEW_STREAM) {
188
- const stream = this._newReceiverStream({ id, name: uint8ArrayToString(message.data instanceof Uint8Array ? message.data : message.data.slice()) });
189
- if (this._options.onStream != null) {
190
- this._options.onStream(stream);
191
- }
192
- return;
193
- }
194
- const list = (type & 1) === 1 ? this._streams.initiators : this._streams.receivers;
195
- const stream = list.get(id);
196
- if (stream == null) {
197
- return log('missing stream %s', id);
198
- }
199
- switch (type) {
200
- case MessageTypes.MESSAGE_INITIATOR:
201
- case MessageTypes.MESSAGE_RECEIVER:
202
- stream.source.push(message.data.slice());
203
- break;
204
- case MessageTypes.CLOSE_INITIATOR:
205
- case MessageTypes.CLOSE_RECEIVER:
206
- stream.close();
207
- break;
208
- case MessageTypes.RESET_INITIATOR:
209
- case MessageTypes.RESET_RECEIVER:
210
- stream.reset();
211
- break;
212
- default:
213
- log('unknown message type %s', type);
214
- }
6
+ createStreamMuxer(components, init) {
7
+ return new MplexStreamMuxer(components, init);
215
8
  }
216
9
  }
217
- Mplex.multicodec = '/mplex/6.7.0';
218
10
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.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;AAMvC,OAAO,IAAI,MAAM,YAAY,CAAA;AAE7B,MAAM,GAAG,GAAG,MAAM,CAAC,cAAc,CAAC,CAAA;AAElC,SAAS,YAAY,CAAE,GAAY;IACjC,MAAM,MAAM,GAAQ;QAClB,GAAG,GAAG;QACN,IAAI,EAAE,GAAG,gBA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,CAAA;IAC/C,CAAC;CACF"}
@@ -0,0 +1,63 @@
1
+ import { Pushable } from 'it-pushable';
2
+ import { Message } from './message-types.js';
3
+ import type { Components } from '@libp2p/interfaces/components';
4
+ import type { Sink } from 'it-stream-types';
5
+ import type { StreamMuxer, StreamMuxerInit } from '@libp2p/interfaces/stream-muxer';
6
+ import type { Stream } from '@libp2p/interfaces/connection';
7
+ export interface MplexStream extends Stream {
8
+ source: Pushable<Uint8Array>;
9
+ }
10
+ export interface MplexInit extends StreamMuxerInit {
11
+ maxMsgSize?: number;
12
+ }
13
+ export declare class MplexStreamMuxer implements StreamMuxer {
14
+ protocol: string;
15
+ sink: Sink<Uint8Array>;
16
+ source: AsyncIterable<Uint8Array>;
17
+ private _streamId;
18
+ private readonly _streams;
19
+ private readonly _init;
20
+ private readonly _source;
21
+ constructor(components: Components, init?: MplexInit);
22
+ init(components: Components): void;
23
+ /**
24
+ * Returns a Map of streams and their ids
25
+ */
26
+ get streams(): Stream[];
27
+ /**
28
+ * Initiate a new stream with the given name. If no name is
29
+ * provided, the id of the stream will be used.
30
+ */
31
+ newStream(name?: string): Stream;
32
+ /**
33
+ * Called whenever an inbound stream is created
34
+ */
35
+ _newReceiverStream(options: {
36
+ id: number;
37
+ name: string;
38
+ }): MplexStream;
39
+ _newStream(options: {
40
+ id: number;
41
+ name: string;
42
+ type: 'initiator' | 'receiver';
43
+ registry: Map<number, MplexStream>;
44
+ }): MplexStream;
45
+ /**
46
+ * Creates a sink with an abortable source. Incoming messages will
47
+ * also have their size restricted. All messages will be varint decoded.
48
+ */
49
+ _createSink(): Sink<Uint8Array, Promise<void>>;
50
+ /**
51
+ * Creates a source that restricts outgoing message sizes
52
+ * and varint encodes them
53
+ */
54
+ _createSource(): AsyncGenerator<Uint8Array, void, undefined> & {
55
+ push: (value: Message) => import("it-pushable").PushableV<Message>;
56
+ end: (err?: Error | undefined) => import("it-pushable").PushableV<Message>;
57
+ return: () => {
58
+ done: boolean;
59
+ };
60
+ };
61
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EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE;IAMzD,UAAU,CAAE,OAAO,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,WAAW,GAAG,UAAU,CAAC;QAAC,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;KAAE;IAmCrH;;;OAGG;IACH,WAAW;IA4BX;;;OAGG;IACH,aAAa;;;;;;;IAoBb,eAAe,CAAE,OAAO,EAAE,OAAO;CA0ClC"}
@@ -0,0 +1,195 @@
1
+ import { pipe } from 'it-pipe';
2
+ import { pushableV } from 'it-pushable';
3
+ import { abortableSource } from 'abortable-iterator';
4
+ import { encode } from './encode.js';
5
+ import { decode } from './decode.js';
6
+ import { restrictSize } from './restrict-size.js';
7
+ import { MessageTypes, MessageTypeNames } from './message-types.js';
8
+ import { createStream } from './stream.js';
9
+ import { toString as uint8ArrayToString } from 'uint8arrays';
10
+ import { trackedMap } from '@libp2p/tracked-map';
11
+ import { logger } from '@libp2p/logger';
12
+ const log = logger('libp2p:mplex');
13
+ function printMessage(msg) {
14
+ const output = {
15
+ ...msg,
16
+ type: `${MessageTypeNames[msg.type]} (${msg.type})`
17
+ };
18
+ if (msg.type === MessageTypes.NEW_STREAM) {
19
+ output.data = uint8ArrayToString(msg.data instanceof Uint8Array ? msg.data : msg.data.slice());
20
+ }
21
+ if (msg.type === MessageTypes.MESSAGE_INITIATOR || msg.type === MessageTypes.MESSAGE_RECEIVER) {
22
+ output.data = uint8ArrayToString(msg.data instanceof Uint8Array ? msg.data : msg.data.slice(), 'base16');
23
+ }
24
+ return output;
25
+ }
26
+ export class MplexStreamMuxer {
27
+ constructor(components, init) {
28
+ this.protocol = '/mplex/6.7.0';
29
+ init = init ?? {};
30
+ this._streamId = 0;
31
+ this._streams = {
32
+ /**
33
+ * Stream to ids map
34
+ */
35
+ initiators: trackedMap({ metrics: components.getMetrics(), component: 'mplex', metric: 'initiatorStreams' }),
36
+ /**
37
+ * Stream to ids map
38
+ */
39
+ receivers: trackedMap({ metrics: components.getMetrics(), component: 'mplex', metric: 'receiverStreams' })
40
+ };
41
+ this._init = init;
42
+ /**
43
+ * An iterable sink
44
+ */
45
+ this.sink = this._createSink();
46
+ /**
47
+ * An iterable source
48
+ */
49
+ const source = this._createSource();
50
+ this._source = source;
51
+ this.source = source;
52
+ }
53
+ init(components) {
54
+ }
55
+ /**
56
+ * Returns a Map of streams and their ids
57
+ */
58
+ get streams() {
59
+ // Inbound and Outbound streams may have the same ids, so we need to make those unique
60
+ const streams = [];
61
+ this._streams.initiators.forEach(stream => {
62
+ streams.push(stream);
63
+ });
64
+ this._streams.receivers.forEach(stream => {
65
+ streams.push(stream);
66
+ });
67
+ return streams;
68
+ }
69
+ /**
70
+ * Initiate a new stream with the given name. If no name is
71
+ * provided, the id of the stream will be used.
72
+ */
73
+ newStream(name) {
74
+ const id = this._streamId++;
75
+ name = name == null ? id.toString() : name.toString();
76
+ const registry = this._streams.initiators;
77
+ return this._newStream({ id, name, type: 'initiator', registry });
78
+ }
79
+ /**
80
+ * Called whenever an inbound stream is created
81
+ */
82
+ _newReceiverStream(options) {
83
+ const { id, name } = options;
84
+ const registry = this._streams.receivers;
85
+ return this._newStream({ id, name, type: 'receiver', registry });
86
+ }
87
+ _newStream(options) {
88
+ const { id, name, type, registry } = options;
89
+ log('new %s stream %s %s', type, id, name);
90
+ if (registry.has(id)) {
91
+ throw new Error(`${type} stream ${id} already exists!`);
92
+ }
93
+ const send = (msg) => {
94
+ if (log.enabled) {
95
+ log.trace('%s stream %s send', type, id, printMessage(msg));
96
+ }
97
+ if (msg.type === MessageTypes.NEW_STREAM || msg.type === MessageTypes.MESSAGE_INITIATOR || msg.type === MessageTypes.MESSAGE_RECEIVER) {
98
+ msg.data = msg.data instanceof Uint8Array ? msg.data : msg.data.slice();
99
+ }
100
+ this._source.push(msg);
101
+ };
102
+ const onEnd = () => {
103
+ log('%s stream %s %s ended', type, id, name);
104
+ registry.delete(id);
105
+ if (this._init.onStreamEnd != null) {
106
+ this._init.onStreamEnd(stream);
107
+ }
108
+ };
109
+ const stream = createStream({ id, name, send, type, onEnd, maxMsgSize: this._init.maxMsgSize });
110
+ registry.set(id, stream);
111
+ return stream;
112
+ }
113
+ /**
114
+ * Creates a sink with an abortable source. Incoming messages will
115
+ * also have their size restricted. All messages will be varint decoded.
116
+ */
117
+ _createSink() {
118
+ const sink = async (source) => {
119
+ if (this._init.signal != null) {
120
+ source = abortableSource(source, this._init.signal);
121
+ }
122
+ try {
123
+ await pipe(source, decode, restrictSize(this._init.maxMsgSize), async (source) => {
124
+ for await (const msg of source) {
125
+ this._handleIncoming(msg);
126
+ }
127
+ });
128
+ this._source.end();
129
+ }
130
+ catch (err) {
131
+ log('error in sink', err);
132
+ this._source.end(err); // End the source with an error
133
+ }
134
+ };
135
+ return sink;
136
+ }
137
+ /**
138
+ * Creates a source that restricts outgoing message sizes
139
+ * and varint encodes them
140
+ */
141
+ _createSource() {
142
+ const onEnd = (err) => {
143
+ const { initiators, receivers } = this._streams;
144
+ // Abort all the things!
145
+ for (const s of initiators.values()) {
146
+ s.abort(err);
147
+ }
148
+ for (const s of receivers.values()) {
149
+ s.abort(err);
150
+ }
151
+ };
152
+ const source = pushableV({ onEnd });
153
+ return Object.assign(encode(source), {
154
+ push: source.push,
155
+ end: source.end,
156
+ return: source.return
157
+ });
158
+ }
159
+ _handleIncoming(message) {
160
+ const { id, type } = message;
161
+ if (log.enabled) {
162
+ log.trace('incoming message', printMessage(message));
163
+ }
164
+ // Create a new stream?
165
+ if (message.type === MessageTypes.NEW_STREAM) {
166
+ const stream = this._newReceiverStream({ id, name: uint8ArrayToString(message.data instanceof Uint8Array ? message.data : message.data.slice()) });
167
+ if (this._init.onIncomingStream != null) {
168
+ this._init.onIncomingStream(stream);
169
+ }
170
+ return;
171
+ }
172
+ const list = (type & 1) === 1 ? this._streams.initiators : this._streams.receivers;
173
+ const stream = list.get(id);
174
+ if (stream == null) {
175
+ return log('missing stream %s', id);
176
+ }
177
+ switch (type) {
178
+ case MessageTypes.MESSAGE_INITIATOR:
179
+ case MessageTypes.MESSAGE_RECEIVER:
180
+ stream.source.push(message.data.slice());
181
+ break;
182
+ case MessageTypes.CLOSE_INITIATOR:
183
+ case MessageTypes.CLOSE_RECEIVER:
184
+ stream.close();
185
+ break;
186
+ case MessageTypes.RESET_INITIATOR:
187
+ case MessageTypes.RESET_RECEIVER:
188
+ stream.reset();
189
+ break;
190
+ default:
191
+ log('unknown message type %s', type);
192
+ }
193
+ }
194
+ }
195
+ //# sourceMappingURL=mplex.js.map
@@ -0,0 +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;AAMvC,MAAM,GAAG,GAAG,MAAM,CAAC,cAAc,CAAC,CAAA;AAElC,SAAS,YAAY,CAAE,GAAY;IACjC,MAAM,MAAM,GAAQ;QAClB,GAAG,GAAG;QACN,IAAI,EAAE,GAAG,gBA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MAAM,CAAC,CAAA;aACpC;YAED,OAAM;SACP;QAED,MAAM,IAAI,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAA;QAClF,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;QAE3B,IAAI,MAAM,IAAI,IAAI,EAAE;YAClB,OAAO,GAAG,CAAC,mBAAmB,EAAE,EAAE,CAAC,CAAA;SACpC;QAED,QAAQ,IAAI,EAAE;YACZ,KAAK,YAAY,CAAC,iBAAiB,CAAC;YACpC,KAAK,YAAY,CAAC,gBAAgB;gBAChC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAA;gBACxC,MAAK;YACP,KAAK,YAAY,CAAC,eAAe,CAAC;YAClC,KAAK,YAAY,CAAC,cAAc;gBAC9B,MAAM,CAAC,KAAK,EAAE,CAAA;gBACd,MAAK;YACP,KAAK,YAAY,CAAC,eAAe,CAAC;YAClC,KAAK,YAAY,CAAC,cAAc;gBAC9B,MAAM,CAAC,KAAK,EAAE,CAAA;gBACd,MAAK;YACP;gBACE,GAAG,CAAC,yBAAyB,EAAE,IAAI,CAAC,CAAA;SACvC;IACH,CAAC;CACF"}
@@ -1,5 +1,5 @@
1
1
  import type { Message } from './message-types.js';
2
- import type { MplexStream } from './index.js';
2
+ import type { MplexStream } from './mplex.js';
3
3
  export interface Options {
4
4
  id: number;
5
5
  send: (msg: Message) => void;
@@ -28,7 +28,7 @@ export function createStream(options) {
28
28
  return;
29
29
  }
30
30
  sourceEnded = true;
31
- log('%s stream %s source end', type, streamName, err);
31
+ log.trace('%s stream %s source end', type, streamName, err);
32
32
  if (err != null && endErr == null) {
33
33
  endErr = err;
34
34
  }
@@ -44,7 +44,7 @@ export function createStream(options) {
44
44
  return;
45
45
  }
46
46
  sinkEnded = true;
47
- log('%s stream %s sink end - err: %o', type, streamName, err);
47
+ log.trace('%s stream %s sink end - err: %o', type, streamName, err);
48
48
  if (err != null && endErr == null) {
49
49
  endErr = err;
50
50
  }
@@ -62,7 +62,7 @@ export function createStream(options) {
62
62
  },
63
63
  // Close for reading and writing (local error)
64
64
  abort: (err) => {
65
- log('%s stream %s abort', type, streamName, err);
65
+ log.trace('%s stream %s abort', type, streamName, err);
66
66
  // End the source with the passed error
67
67
  stream.source.end(err);
68
68
  abortController.abort();
@@ -112,15 +112,15 @@ export function createStream(options) {
112
112
  }
113
113
  // Send no more data if this stream was remotely reset
114
114
  if (err.code === ERR_MPLEX_STREAM_RESET) {
115
- log('%s stream %s reset', type, name);
115
+ log.trace('%s stream %s reset', type, name);
116
116
  }
117
117
  else {
118
- log('%s stream %s error', type, name, err);
118
+ log.trace('%s stream %s error', type, name, err);
119
119
  try {
120
120
  send({ id, type: Types.RESET });
121
121
  }
122
122
  catch (err) {
123
- log('%s stream %s error sending reset', type, name, err);
123
+ log.trace('%s stream %s error sending reset', type, name, err);
124
124
  }
125
125
  }
126
126
  stream.source.end(err);
@@ -131,7 +131,7 @@ export function createStream(options) {
131
131
  send({ id, type: Types.CLOSE });
132
132
  }
133
133
  catch (err) {
134
- log('%s stream %s error sending close', type, name, err);
134
+ log.trace('%s stream %s error sending close', type, name, err);
135
135
  }
136
136
  onSinkEnd();
137
137
  },