@@ -20,12 +20,14 @@ log.error = debug('libp2p-pubsub:peer-streams:error')
20
20
* @param {Uint8Array } source
21
21
* @returns {Promise<Uint8Array> }
22
22
*
23
- * @typedef {object } DuplexIterableStream
24
- * @property {Sink } sink
25
- * @property {AsyncIterator<Uint8Array> } source
23
+ * @typedef {object } MuxedStream
24
+ * @type import('../stream-muxer/types').MuxedStream
26
25
*
27
26
* @typedef PeerId
28
27
* @type import('peer-id')
28
+ *
29
+ * @typedef PushableStream
30
+ * @type import('it-pushable').Pushable<Uint8Array>
29
31
*/
30
32
31
33
/**
@@ -54,33 +56,33 @@ class PeerStreams extends EventEmitter {
54
56
* The raw outbound stream, as retrieved from conn.newStream
55
57
*
56
58
* @private
57
- * @type {null|DuplexIterableStream }
59
+ * @type {null|MuxedStream }
58
60
*/
59
61
this . _rawOutboundStream = null
60
62
/**
61
63
* The raw inbound stream, as retrieved from the callback from libp2p.handle
62
64
*
63
65
* @private
64
- * @type {null|DuplexIterableStream }
66
+ * @type {null|MuxedStream }
65
67
*/
66
68
this . _rawInboundStream = null
67
69
/**
68
70
* An AbortController for controlled shutdown of the inbound stream
69
71
*
70
72
* @private
71
- * @type {null| AbortController }
73
+ * @type {AbortController }
72
74
*/
73
- this . _inboundAbortController = null
75
+ this . _inboundAbortController = new AbortController ( )
74
76
/**
75
77
* Write stream -- its preferable to use the write method
76
78
*
77
- * @type {null|import('it-pushable').Pushable<Uint8Array> }
79
+ * @type {null|PushableStream }
78
80
*/
79
81
this . outboundStream = null
80
82
/**
81
83
* Read stream
82
84
*
83
- * @type {null|DuplexIterableStream }
85
+ * @type {null|MuxedStream }
84
86
*/
85
87
this . inboundStream = null
86
88
}
@@ -123,23 +125,21 @@ class PeerStreams extends EventEmitter {
123
125
/**
124
126
* Attach a raw inbound stream and setup a read stream
125
127
*
126
- * @param {DuplexIterableStream } stream
128
+ * @param {MuxedStream } stream
127
129
* @returns {void }
128
130
*/
129
131
attachInboundStream ( stream ) {
130
132
// Create and attach a new inbound stream
131
133
// The inbound stream is:
132
134
// - abortable, set to only return on abort, rather than throw
133
135
// - transformed with length-prefix transform
134
- this . _inboundAbortController = new AbortController ( )
135
136
this . _rawInboundStream = stream
136
- // @ts -ignore - abortable returns AsyncIterable and not a DuplexIterableStream
137
+ // @ts -ignore - abortable returns AsyncIterable and not a MuxedStream
137
138
this . inboundStream = abortable (
138
139
pipe (
139
140
this . _rawInboundStream ,
140
141
lp . decode ( )
141
142
) ,
142
- // @ts -ignore - possibly null
143
143
this . _inboundAbortController . signal ,
144
144
{ returnOnAbort : true }
145
145
)
@@ -150,30 +150,26 @@ class PeerStreams extends EventEmitter {
150
150
/**
151
151
* Attach a raw outbound stream and setup a write stream
152
152
*
153
- * @param {DuplexIterableStream } stream
153
+ * @param {MuxedStream } stream
154
154
* @returns {Promise<void> }
155
155
*/
156
156
async attachOutboundStream ( stream ) {
157
157
// If an outbound stream already exists,
158
158
// gently close it
159
159
const _prevStream = this . outboundStream
160
- if ( _prevStream ) {
160
+ if ( this . outboundStream ) {
161
161
// End the stream without emitting a close event
162
- // @ts -ignore - outboundStream may be null
163
- await this . outboundStream . end ( false )
162
+ await this . outboundStream . end ( )
164
163
}
165
164
166
165
this . _rawOutboundStream = stream
167
166
this . outboundStream = pushable ( {
168
167
onEnd : ( shouldEmit ) => {
169
168
// close writable side of the stream
170
- // @ts -ignore - DuplexIterableStream does not define reset
171
169
this . _rawOutboundStream && this . _rawOutboundStream . reset && this . _rawOutboundStream . reset ( )
172
170
this . _rawOutboundStream = null
173
171
this . outboundStream = null
174
- // @ts -ignore - shouldEmit is `Error | undefined` so condition is
175
- // always false
176
- if ( shouldEmit !== false ) {
172
+ if ( shouldEmit ) {
177
173
this . emit ( 'close' )
178
174
}
179
175
}
@@ -205,7 +201,6 @@ class PeerStreams extends EventEmitter {
205
201
}
206
202
// End the inbound stream
207
203
if ( this . inboundStream ) {
208
- // @ts -ignore - possibly null
209
204
this . _inboundAbortController . abort ( )
210
205
}
211
206
0 commit comments