@libp2p/interface 0.1.2 → 0.1.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.
- package/dist/src/connection/index.d.ts +13 -13
- package/dist/src/connection/index.d.ts.map +1 -1
- package/dist/src/connection-encrypter/index.d.ts +2 -2
- package/dist/src/connection-encrypter/index.d.ts.map +1 -1
- package/dist/src/connection-gater/index.d.ts +12 -12
- package/dist/src/connection-gater/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.d.ts +4 -4
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/index.d.ts +13 -13
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/keychain/index.d.ts +11 -11
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keys/index.d.ts +10 -10
- package/dist/src/keys/index.d.ts.map +1 -1
- package/dist/src/metrics/index.d.ts +16 -16
- package/dist/src/metrics/index.d.ts.map +1 -1
- package/dist/src/peer-id/index.d.ts +4 -4
- package/dist/src/peer-id/index.d.ts.map +1 -1
- package/dist/src/peer-routing/index.d.ts +2 -2
- package/dist/src/peer-routing/index.d.ts.map +1 -1
- package/dist/src/peer-store/index.d.ts +9 -9
- package/dist/src/peer-store/index.d.ts.map +1 -1
- package/dist/src/pubsub/index.d.ts +10 -10
- package/dist/src/pubsub/index.d.ts.map +1 -1
- package/dist/src/record/index.d.ts +5 -5
- package/dist/src/record/index.d.ts.map +1 -1
- package/dist/src/startable.d.ts +7 -7
- package/dist/src/startable.d.ts.map +1 -1
- package/dist/src/stream-muxer/index.d.ts +6 -6
- package/dist/src/stream-muxer/index.d.ts.map +1 -1
- package/dist/src/stream-muxer/stream.d.ts +13 -7
- package/dist/src/stream-muxer/stream.d.ts.map +1 -1
- package/dist/src/stream-muxer/stream.js +31 -19
- package/dist/src/stream-muxer/stream.js.map +1 -1
- package/dist/src/topology/index.d.ts +2 -2
- package/dist/src/topology/index.d.ts.map +1 -1
- package/dist/src/transport/index.d.ts +7 -7
- package/dist/src/transport/index.d.ts.map +1 -1
- package/dist/typedoc-urls.json +133 -8
- package/package.json +8 -3
- package/src/connection/index.ts +13 -13
- package/src/connection-encrypter/index.ts +2 -2
- package/src/connection-gater/index.ts +12 -12
- package/src/content-routing/index.ts +4 -4
- package/src/index.ts +13 -13
- package/src/keychain/index.ts +11 -11
- package/src/keys/index.ts +10 -10
- package/src/metrics/index.ts +16 -16
- package/src/peer-id/index.ts +4 -4
- package/src/peer-routing/index.ts +2 -2
- package/src/peer-store/index.ts +9 -9
- package/src/pubsub/index.ts +10 -10
- package/src/record/index.ts +5 -5
- package/src/startable.ts +7 -7
- package/src/stream-muxer/index.ts +6 -6
- package/src/stream-muxer/stream.ts +52 -30
- package/src/topology/index.ts +2 -2
- package/src/transport/index.ts +7 -7
|
@@ -34,7 +34,7 @@ export interface PeerRouting {
|
|
|
34
34
|
* const peer = await peerRouting.findPeer(peerId, options)
|
|
35
35
|
* ```
|
|
36
36
|
*/
|
|
37
|
-
findPeer
|
|
37
|
+
findPeer(peerId: PeerId, options?: AbortOptions): Promise<PeerInfo>
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
40
|
* Search the network for peers that are closer to the passed key. Peer
|
|
@@ -49,5 +49,5 @@ export interface PeerRouting {
|
|
|
49
49
|
* }
|
|
50
50
|
* ```
|
|
51
51
|
*/
|
|
52
|
-
getClosestPeers
|
|
52
|
+
getClosestPeers(key: Uint8Array, options?: AbortOptions): AsyncIterable<PeerInfo>
|
|
53
53
|
}
|
package/src/peer-store/index.ts
CHANGED
|
@@ -156,7 +156,7 @@ export interface PeerStore {
|
|
|
156
156
|
* })
|
|
157
157
|
* ```
|
|
158
158
|
*/
|
|
159
|
-
forEach
|
|
159
|
+
forEach(fn: (peer: Peer) => void, query?: PeerQuery): Promise<void>
|
|
160
160
|
|
|
161
161
|
/**
|
|
162
162
|
* Returns all peers in the peer store.
|
|
@@ -169,7 +169,7 @@ export interface PeerStore {
|
|
|
169
169
|
* }
|
|
170
170
|
* ```
|
|
171
171
|
*/
|
|
172
|
-
all
|
|
172
|
+
all(query?: PeerQuery): Promise<Peer[]>
|
|
173
173
|
|
|
174
174
|
/**
|
|
175
175
|
* Delete all data stored for the passed peer
|
|
@@ -187,7 +187,7 @@ export interface PeerStore {
|
|
|
187
187
|
* // []
|
|
188
188
|
* ```
|
|
189
189
|
*/
|
|
190
|
-
delete
|
|
190
|
+
delete(peerId: PeerId): Promise<void>
|
|
191
191
|
|
|
192
192
|
/**
|
|
193
193
|
* Returns true if the passed PeerId is in the peer store
|
|
@@ -202,7 +202,7 @@ export interface PeerStore {
|
|
|
202
202
|
* // true
|
|
203
203
|
* ```
|
|
204
204
|
*/
|
|
205
|
-
has
|
|
205
|
+
has(peerId: PeerId): Promise<boolean>
|
|
206
206
|
|
|
207
207
|
/**
|
|
208
208
|
* Returns all data stored for the passed PeerId
|
|
@@ -214,7 +214,7 @@ export interface PeerStore {
|
|
|
214
214
|
* // { .. }
|
|
215
215
|
* ```
|
|
216
216
|
*/
|
|
217
|
-
get
|
|
217
|
+
get(peerId: PeerId): Promise<Peer>
|
|
218
218
|
|
|
219
219
|
/**
|
|
220
220
|
* Adds a peer to the peer store, overwriting any existing data
|
|
@@ -227,7 +227,7 @@ export interface PeerStore {
|
|
|
227
227
|
* })
|
|
228
228
|
* ```
|
|
229
229
|
*/
|
|
230
|
-
save
|
|
230
|
+
save(id: PeerId, data: PeerData): Promise<Peer>
|
|
231
231
|
|
|
232
232
|
/**
|
|
233
233
|
* Adds a peer to the peer store, overwriting only the passed fields
|
|
@@ -240,7 +240,7 @@ export interface PeerStore {
|
|
|
240
240
|
* })
|
|
241
241
|
* ```
|
|
242
242
|
*/
|
|
243
|
-
patch
|
|
243
|
+
patch(id: PeerId, data: PeerData): Promise<Peer>
|
|
244
244
|
|
|
245
245
|
/**
|
|
246
246
|
* Adds a peer to the peer store, deeply merging any existing data.
|
|
@@ -253,7 +253,7 @@ export interface PeerStore {
|
|
|
253
253
|
* })
|
|
254
254
|
* ```
|
|
255
255
|
*/
|
|
256
|
-
merge
|
|
256
|
+
merge(id: PeerId, data: PeerData): Promise<Peer>
|
|
257
257
|
|
|
258
258
|
/**
|
|
259
259
|
* Unmarshal and verify a signed peer record, extract the multiaddrs and
|
|
@@ -268,5 +268,5 @@ export interface PeerStore {
|
|
|
268
268
|
* await peerStore.consumePeerRecord(buf, expectedPeer)
|
|
269
269
|
* ```
|
|
270
270
|
*/
|
|
271
|
-
consumePeerRecord
|
|
271
|
+
consumePeerRecord(buf: Uint8Array, expectedPeer?: PeerId): Promise<boolean>
|
|
272
272
|
}
|
package/src/pubsub/index.ts
CHANGED
|
@@ -72,10 +72,10 @@ export interface PeerStreams extends EventEmitter<PeerStreamEvents> {
|
|
|
72
72
|
inboundStream?: AsyncIterable<Uint8ArrayList>
|
|
73
73
|
isWritable: boolean
|
|
74
74
|
|
|
75
|
-
close
|
|
76
|
-
write
|
|
77
|
-
attachInboundStream
|
|
78
|
-
attachOutboundStream
|
|
75
|
+
close(): void
|
|
76
|
+
write(buf: Uint8Array | Uint8ArrayList): void
|
|
77
|
+
attachInboundStream(stream: Stream): AsyncIterable<Uint8ArrayList>
|
|
78
|
+
attachOutboundStream(stream: Stream): Promise<Pushable<Uint8ArrayList>>
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
export interface PubSubInit {
|
|
@@ -189,7 +189,7 @@ export interface PubSub<Events extends Record<string, any> = PubSubEvents> exten
|
|
|
189
189
|
*/
|
|
190
190
|
topicValidators: Map<string, TopicValidatorFn>
|
|
191
191
|
|
|
192
|
-
getPeers
|
|
192
|
+
getPeers(): PeerId[]
|
|
193
193
|
|
|
194
194
|
/**
|
|
195
195
|
* Gets a list of topics the node is subscribed to.
|
|
@@ -198,7 +198,7 @@ export interface PubSub<Events extends Record<string, any> = PubSubEvents> exten
|
|
|
198
198
|
* const topics = libp2p.pubsub.getTopics()
|
|
199
199
|
* ```
|
|
200
200
|
*/
|
|
201
|
-
getTopics
|
|
201
|
+
getTopics(): string[]
|
|
202
202
|
|
|
203
203
|
/**
|
|
204
204
|
* Subscribes to a pubsub topic.
|
|
@@ -217,7 +217,7 @@ export interface PubSub<Events extends Record<string, any> = PubSubEvents> exten
|
|
|
217
217
|
* libp2p.pubsub.subscribe(topic)
|
|
218
218
|
* ```
|
|
219
219
|
*/
|
|
220
|
-
subscribe
|
|
220
|
+
subscribe(topic: string): void
|
|
221
221
|
|
|
222
222
|
/**
|
|
223
223
|
* Unsubscribes from a pubsub topic.
|
|
@@ -234,7 +234,7 @@ export interface PubSub<Events extends Record<string, any> = PubSubEvents> exten
|
|
|
234
234
|
* libp2p.pubsub.unsubscribe(topic)
|
|
235
235
|
* ```
|
|
236
236
|
*/
|
|
237
|
-
unsubscribe
|
|
237
|
+
unsubscribe(topic: string): void
|
|
238
238
|
|
|
239
239
|
/**
|
|
240
240
|
* Gets a list of the PeerIds that are subscribed to one topic.
|
|
@@ -245,7 +245,7 @@ export interface PubSub<Events extends Record<string, any> = PubSubEvents> exten
|
|
|
245
245
|
* const peerIds = libp2p.pubsub.getSubscribers(topic)
|
|
246
246
|
* ```
|
|
247
247
|
*/
|
|
248
|
-
getSubscribers
|
|
248
|
+
getSubscribers(topic: string): PeerId[]
|
|
249
249
|
|
|
250
250
|
/**
|
|
251
251
|
* Publishes messages to the given topic.
|
|
@@ -259,7 +259,7 @@ export interface PubSub<Events extends Record<string, any> = PubSubEvents> exten
|
|
|
259
259
|
* await libp2p.pubsub.publish(topic, data)
|
|
260
260
|
* ```
|
|
261
261
|
*/
|
|
262
|
-
publish
|
|
262
|
+
publish(topic: string, data: Uint8Array): Promise<PublishResult>
|
|
263
263
|
}
|
|
264
264
|
|
|
265
265
|
export interface PeerStreamEvents {
|
package/src/record/index.ts
CHANGED
|
@@ -16,11 +16,11 @@ export interface Record {
|
|
|
16
16
|
/**
|
|
17
17
|
* Marshal a record to be used in an envelope.
|
|
18
18
|
*/
|
|
19
|
-
marshal
|
|
19
|
+
marshal(): Uint8Array
|
|
20
20
|
/**
|
|
21
21
|
* Verifies if the other provided Record is identical to this one.
|
|
22
22
|
*/
|
|
23
|
-
equals
|
|
23
|
+
equals(other: Record): boolean
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export interface Envelope {
|
|
@@ -29,7 +29,7 @@ export interface Envelope {
|
|
|
29
29
|
payload: Uint8Array
|
|
30
30
|
signature: Uint8Array | Uint8ArrayList
|
|
31
31
|
|
|
32
|
-
marshal
|
|
33
|
-
validate
|
|
34
|
-
equals
|
|
32
|
+
marshal(): Uint8Array
|
|
33
|
+
validate(domain: string): Promise<boolean>
|
|
34
|
+
equals(other: Envelope): boolean
|
|
35
35
|
}
|
package/src/startable.ts
CHANGED
|
@@ -2,49 +2,49 @@
|
|
|
2
2
|
* Implemented by components that have a lifecycle
|
|
3
3
|
*/
|
|
4
4
|
export interface Startable {
|
|
5
|
-
isStarted
|
|
5
|
+
isStarted(): boolean
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* If implemented, this method will be invoked before the start method.
|
|
9
9
|
*
|
|
10
10
|
* It should not assume any other components have been started.
|
|
11
11
|
*/
|
|
12
|
-
beforeStart
|
|
12
|
+
beforeStart?(): void | Promise<void>
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* This method will be invoked to start the component.
|
|
16
16
|
*
|
|
17
17
|
* It should not assume that any other components have been started.
|
|
18
18
|
*/
|
|
19
|
-
start
|
|
19
|
+
start(): void | Promise<void>
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* If implemented, this method will be invoked after the start method.
|
|
23
23
|
*
|
|
24
24
|
* All other components will have had their start method invoked before this method is called.
|
|
25
25
|
*/
|
|
26
|
-
afterStart
|
|
26
|
+
afterStart?(): void | Promise<void>
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* If implemented, this method will be invoked before the stop method.
|
|
30
30
|
*
|
|
31
31
|
* Any other components will still be running when this method is called.
|
|
32
32
|
*/
|
|
33
|
-
beforeStop
|
|
33
|
+
beforeStop?(): void | Promise<void>
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* This method will be invoked to stop the component.
|
|
37
37
|
*
|
|
38
38
|
* It should not assume any other components are running when it is called.
|
|
39
39
|
*/
|
|
40
|
-
stop
|
|
40
|
+
stop(): void | Promise<void>
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
43
|
* If implemented, this method will be invoked after the stop method.
|
|
44
44
|
*
|
|
45
45
|
* All other components will have had their stop method invoked before this method is called.
|
|
46
46
|
*/
|
|
47
|
-
afterStop
|
|
47
|
+
afterStop?(): void | Promise<void>
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
export function isStartable (obj: any): obj is Startable {
|
|
@@ -12,7 +12,7 @@ export interface StreamMuxerFactory {
|
|
|
12
12
|
/**
|
|
13
13
|
* Creates a new stream muxer to be used with a new connection
|
|
14
14
|
*/
|
|
15
|
-
createStreamMuxer
|
|
15
|
+
createStreamMuxer(init?: StreamMuxerInit): StreamMuxer
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/**
|
|
@@ -32,29 +32,29 @@ export interface StreamMuxer extends Duplex<AsyncGenerator<Uint8Array>, Source<U
|
|
|
32
32
|
* Initiate a new stream with the given name. If no name is
|
|
33
33
|
* provided, the id of the stream will be used.
|
|
34
34
|
*/
|
|
35
|
-
newStream
|
|
35
|
+
newStream(name?: string): Stream | Promise<Stream>
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
38
|
* Close or abort all tracked streams and stop the muxer
|
|
39
39
|
*/
|
|
40
|
-
close
|
|
40
|
+
close(options?: AbortOptions): Promise<void>
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
43
|
* Close or abort all tracked streams and stop the muxer
|
|
44
44
|
*/
|
|
45
|
-
abort
|
|
45
|
+
abort(err: Error): void
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export interface StreamMuxerInit {
|
|
49
49
|
/**
|
|
50
50
|
* A callback function invoked every time an incoming stream is opened
|
|
51
51
|
*/
|
|
52
|
-
onIncomingStream
|
|
52
|
+
onIncomingStream?(stream: Stream): void
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
55
|
* A callback function invoke every time a stream ends
|
|
56
56
|
*/
|
|
57
|
-
onStreamEnd
|
|
57
|
+
onStreamEnd?(stream: Stream): void
|
|
58
58
|
|
|
59
59
|
/**
|
|
60
60
|
* Outbound stream muxers are opened by the local node, inbound stream muxers are opened by the remote
|
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
import { abortableSource } from 'abortable-iterator'
|
|
2
2
|
import { type Pushable, pushable } from 'it-pushable'
|
|
3
3
|
import defer, { type DeferredPromise } from 'p-defer'
|
|
4
|
+
import { raceSignal } from 'race-signal'
|
|
4
5
|
import { Uint8ArrayList } from 'uint8arraylist'
|
|
5
6
|
import { CodeError } from '../errors.js'
|
|
6
7
|
import type { Direction, ReadStatus, Stream, StreamStatus, StreamTimeline, WriteStatus } from '../connection/index.js'
|
|
7
8
|
import type { AbortOptions } from '../index.js'
|
|
8
9
|
import type { Source } from 'it-stream-types'
|
|
9
10
|
|
|
11
|
+
// copied from @libp2p/logger to break a circular dependency
|
|
10
12
|
interface Logger {
|
|
11
13
|
(formatter: any, ...args: any[]): void
|
|
12
|
-
error
|
|
13
|
-
trace
|
|
14
|
+
error(formatter: any, ...args: any[]): void
|
|
15
|
+
trace(formatter: any, ...args: any[]): void
|
|
14
16
|
enabled: boolean
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
const ERR_STREAM_RESET = 'ERR_STREAM_RESET'
|
|
18
20
|
const ERR_SINK_INVALID_STATE = 'ERR_SINK_INVALID_STATE'
|
|
21
|
+
const DEFAULT_SEND_CLOSE_WRITE_TIMEOUT = 5000
|
|
19
22
|
|
|
20
23
|
export interface AbstractStreamInit {
|
|
21
24
|
/**
|
|
@@ -41,33 +44,39 @@ export interface AbstractStreamInit {
|
|
|
41
44
|
/**
|
|
42
45
|
* Invoked when the stream ends
|
|
43
46
|
*/
|
|
44
|
-
onEnd
|
|
47
|
+
onEnd?(err?: Error | undefined): void
|
|
45
48
|
|
|
46
49
|
/**
|
|
47
50
|
* Invoked when the readable end of the stream is closed
|
|
48
51
|
*/
|
|
49
|
-
onCloseRead
|
|
52
|
+
onCloseRead?(): void
|
|
50
53
|
|
|
51
54
|
/**
|
|
52
55
|
* Invoked when the writable end of the stream is closed
|
|
53
56
|
*/
|
|
54
|
-
onCloseWrite
|
|
57
|
+
onCloseWrite?(): void
|
|
55
58
|
|
|
56
59
|
/**
|
|
57
60
|
* Invoked when the the stream has been reset by the remote
|
|
58
61
|
*/
|
|
59
|
-
onReset
|
|
62
|
+
onReset?(): void
|
|
60
63
|
|
|
61
64
|
/**
|
|
62
65
|
* Invoked when the the stream has errored
|
|
63
66
|
*/
|
|
64
|
-
onAbort
|
|
67
|
+
onAbort?(err: Error): void
|
|
65
68
|
|
|
66
69
|
/**
|
|
67
70
|
* How long to wait in ms for stream data to be written to the underlying
|
|
68
71
|
* connection when closing the writable end of the stream. (default: 500)
|
|
69
72
|
*/
|
|
70
73
|
closeTimeout?: number
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* After the stream sink has closed, a limit on how long it takes to send
|
|
77
|
+
* a close-write message to the remote peer.
|
|
78
|
+
*/
|
|
79
|
+
sendCloseWriteTimeout?: number
|
|
71
80
|
}
|
|
72
81
|
|
|
73
82
|
function isPromise (res?: any): res is Promise<void> {
|
|
@@ -94,6 +103,7 @@ export abstract class AbstractStream implements Stream {
|
|
|
94
103
|
private readonly onCloseWrite?: () => void
|
|
95
104
|
private readonly onReset?: () => void
|
|
96
105
|
private readonly onAbort?: (err: Error) => void
|
|
106
|
+
private readonly sendCloseWriteTimeout: number
|
|
97
107
|
|
|
98
108
|
protected readonly log: Logger
|
|
99
109
|
|
|
@@ -113,6 +123,7 @@ export abstract class AbstractStream implements Stream {
|
|
|
113
123
|
this.timeline = {
|
|
114
124
|
open: Date.now()
|
|
115
125
|
}
|
|
126
|
+
this.sendCloseWriteTimeout = init.sendCloseWriteTimeout ?? DEFAULT_SEND_CLOSE_WRITE_TIMEOUT
|
|
116
127
|
|
|
117
128
|
this.onEnd = init.onEnd
|
|
118
129
|
this.onCloseRead = init?.onCloseRead
|
|
@@ -128,7 +139,6 @@ export abstract class AbstractStream implements Stream {
|
|
|
128
139
|
this.log.trace('source ended')
|
|
129
140
|
}
|
|
130
141
|
|
|
131
|
-
this.readStatus = 'closed'
|
|
132
142
|
this.onSourceEnd(err)
|
|
133
143
|
}
|
|
134
144
|
})
|
|
@@ -173,11 +183,19 @@ export abstract class AbstractStream implements Stream {
|
|
|
173
183
|
}
|
|
174
184
|
}
|
|
175
185
|
|
|
176
|
-
this.log.trace('sink finished reading from source')
|
|
177
|
-
|
|
186
|
+
this.log.trace('sink finished reading from source, write status is "%s"', this.writeStatus)
|
|
187
|
+
|
|
188
|
+
if (this.writeStatus === 'writing') {
|
|
189
|
+
this.writeStatus = 'closing'
|
|
190
|
+
|
|
191
|
+
this.log.trace('send close write to remote')
|
|
192
|
+
await this.sendCloseWrite({
|
|
193
|
+
signal: AbortSignal.timeout(this.sendCloseWriteTimeout)
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
this.writeStatus = 'closed'
|
|
197
|
+
}
|
|
178
198
|
|
|
179
|
-
this.log.trace('sink calling closeWrite')
|
|
180
|
-
await this.closeWrite(options)
|
|
181
199
|
this.onSinkEnd()
|
|
182
200
|
} catch (err: any) {
|
|
183
201
|
this.log.trace('sink ended with error, calling abort with error', err)
|
|
@@ -196,6 +214,7 @@ export abstract class AbstractStream implements Stream {
|
|
|
196
214
|
}
|
|
197
215
|
|
|
198
216
|
this.timeline.closeRead = Date.now()
|
|
217
|
+
this.readStatus = 'closed'
|
|
199
218
|
|
|
200
219
|
if (err != null && this.endErr == null) {
|
|
201
220
|
this.endErr = err
|
|
@@ -207,6 +226,10 @@ export abstract class AbstractStream implements Stream {
|
|
|
207
226
|
this.log.trace('source and sink ended')
|
|
208
227
|
this.timeline.close = Date.now()
|
|
209
228
|
|
|
229
|
+
if (this.status !== 'aborted' && this.status !== 'reset') {
|
|
230
|
+
this.status = 'closed'
|
|
231
|
+
}
|
|
232
|
+
|
|
210
233
|
if (this.onEnd != null) {
|
|
211
234
|
this.onEnd(this.endErr)
|
|
212
235
|
}
|
|
@@ -221,6 +244,7 @@ export abstract class AbstractStream implements Stream {
|
|
|
221
244
|
}
|
|
222
245
|
|
|
223
246
|
this.timeline.closeWrite = Date.now()
|
|
247
|
+
this.writeStatus = 'closed'
|
|
224
248
|
|
|
225
249
|
if (err != null && this.endErr == null) {
|
|
226
250
|
this.endErr = err
|
|
@@ -232,6 +256,10 @@ export abstract class AbstractStream implements Stream {
|
|
|
232
256
|
this.log.trace('sink and source ended')
|
|
233
257
|
this.timeline.close = Date.now()
|
|
234
258
|
|
|
259
|
+
if (this.status !== 'aborted' && this.status !== 'reset') {
|
|
260
|
+
this.status = 'closed'
|
|
261
|
+
}
|
|
262
|
+
|
|
235
263
|
if (this.onEnd != null) {
|
|
236
264
|
this.onEnd(this.endErr)
|
|
237
265
|
}
|
|
@@ -266,16 +294,16 @@ export abstract class AbstractStream implements Stream {
|
|
|
266
294
|
const readStatus = this.readStatus
|
|
267
295
|
this.readStatus = 'closing'
|
|
268
296
|
|
|
269
|
-
if (readStatus === 'ready') {
|
|
270
|
-
this.log.trace('ending internal source queue')
|
|
271
|
-
this.streamSource.end()
|
|
272
|
-
}
|
|
273
|
-
|
|
274
297
|
if (this.status !== 'reset' && this.status !== 'aborted' && this.timeline.closeRead == null) {
|
|
275
298
|
this.log.trace('send close read to remote')
|
|
276
299
|
await this.sendCloseRead(options)
|
|
277
300
|
}
|
|
278
301
|
|
|
302
|
+
if (readStatus === 'ready') {
|
|
303
|
+
this.log.trace('ending internal source queue')
|
|
304
|
+
this.streamSource.end()
|
|
305
|
+
}
|
|
306
|
+
|
|
279
307
|
this.log.trace('closed readable end of stream')
|
|
280
308
|
}
|
|
281
309
|
|
|
@@ -286,16 +314,13 @@ export abstract class AbstractStream implements Stream {
|
|
|
286
314
|
|
|
287
315
|
this.log.trace('closing writable end of stream with starting write status "%s"', this.writeStatus)
|
|
288
316
|
|
|
289
|
-
const writeStatus = this.writeStatus
|
|
290
|
-
|
|
291
317
|
if (this.writeStatus === 'ready') {
|
|
292
318
|
this.log.trace('sink was never sunk, sink an empty array')
|
|
293
|
-
await this.sink([])
|
|
294
|
-
}
|
|
295
319
|
|
|
296
|
-
|
|
320
|
+
await raceSignal(this.sink([]), options.signal)
|
|
321
|
+
}
|
|
297
322
|
|
|
298
|
-
if (writeStatus === 'writing') {
|
|
323
|
+
if (this.writeStatus === 'writing') {
|
|
299
324
|
// stop reading from the source passed to `.sink` in the microtask queue
|
|
300
325
|
// - this lets any data queued by the user in the current tick get read
|
|
301
326
|
// before we exit
|
|
@@ -303,16 +328,12 @@ export abstract class AbstractStream implements Stream {
|
|
|
303
328
|
queueMicrotask(() => {
|
|
304
329
|
this.log.trace('aborting source passed to .sink')
|
|
305
330
|
this.sinkController.abort()
|
|
306
|
-
this.sinkEnd.promise
|
|
331
|
+
raceSignal(this.sinkEnd.promise, options.signal)
|
|
332
|
+
.then(resolve, reject)
|
|
307
333
|
})
|
|
308
334
|
})
|
|
309
335
|
}
|
|
310
336
|
|
|
311
|
-
if (this.status !== 'reset' && this.status !== 'aborted' && this.timeline.closeWrite == null) {
|
|
312
|
-
this.log.trace('send close write to remote')
|
|
313
|
-
await this.sendCloseWrite(options)
|
|
314
|
-
}
|
|
315
|
-
|
|
316
337
|
this.writeStatus = 'closed'
|
|
317
338
|
|
|
318
339
|
this.log.trace('closed writable end of stream')
|
|
@@ -357,6 +378,7 @@ export abstract class AbstractStream implements Stream {
|
|
|
357
378
|
const err = new CodeError('stream reset', ERR_STREAM_RESET)
|
|
358
379
|
|
|
359
380
|
this.status = 'reset'
|
|
381
|
+
this.timeline.reset = Date.now()
|
|
360
382
|
this._closeSinkAndSource(err)
|
|
361
383
|
this.onReset?.()
|
|
362
384
|
}
|
|
@@ -423,7 +445,7 @@ export abstract class AbstractStream implements Stream {
|
|
|
423
445
|
return
|
|
424
446
|
}
|
|
425
447
|
|
|
426
|
-
this.log.trace('
|
|
448
|
+
this.log.trace('stream destroyed')
|
|
427
449
|
|
|
428
450
|
this._closeSinkAndSource()
|
|
429
451
|
}
|
package/src/topology/index.ts
CHANGED
package/src/transport/index.ts
CHANGED
|
@@ -15,17 +15,17 @@ export interface Listener extends EventEmitter<ListenerEvents> {
|
|
|
15
15
|
/**
|
|
16
16
|
* Start a listener
|
|
17
17
|
*/
|
|
18
|
-
listen
|
|
18
|
+
listen(multiaddr: Multiaddr): Promise<void>
|
|
19
19
|
/**
|
|
20
20
|
* Get listen addresses
|
|
21
21
|
*/
|
|
22
|
-
getAddrs
|
|
22
|
+
getAddrs(): Multiaddr[]
|
|
23
23
|
/**
|
|
24
24
|
* Close listener
|
|
25
25
|
*
|
|
26
26
|
* @returns {Promise<void>}
|
|
27
27
|
*/
|
|
28
|
-
close
|
|
28
|
+
close(): Promise<void>
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
export const symbol = Symbol.for('@libp2p/transport')
|
|
@@ -60,12 +60,12 @@ export interface Transport {
|
|
|
60
60
|
/**
|
|
61
61
|
* Dial a given multiaddr.
|
|
62
62
|
*/
|
|
63
|
-
dial
|
|
63
|
+
dial(ma: Multiaddr, options: DialOptions): Promise<Connection>
|
|
64
64
|
|
|
65
65
|
/**
|
|
66
66
|
* Create transport listeners.
|
|
67
67
|
*/
|
|
68
|
-
createListener
|
|
68
|
+
createListener(options: CreateListenerOptions): Listener
|
|
69
69
|
|
|
70
70
|
/**
|
|
71
71
|
* Takes a list of `Multiaddr`s and returns only valid addresses for the transport
|
|
@@ -108,10 +108,10 @@ export interface Upgrader {
|
|
|
108
108
|
/**
|
|
109
109
|
* Upgrades an outbound connection on `transport.dial`.
|
|
110
110
|
*/
|
|
111
|
-
upgradeOutbound
|
|
111
|
+
upgradeOutbound(maConn: MultiaddrConnection, opts?: UpgraderOptions): Promise<Connection>
|
|
112
112
|
|
|
113
113
|
/**
|
|
114
114
|
* Upgrades an inbound connection on transport listener.
|
|
115
115
|
*/
|
|
116
|
-
upgradeInbound
|
|
116
|
+
upgradeInbound(maConn: MultiaddrConnection, opts?: UpgraderOptions): Promise<Connection>
|
|
117
117
|
}
|