libp2p 2.8.14 → 2.9.0-79473c99a
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/index.min.js +15 -15
- package/dist/index.min.js.map +4 -4
- package/dist/src/address-manager/dns-mappings.js +1 -1
- package/dist/src/address-manager/dns-mappings.js.map +1 -1
- package/dist/src/connection-manager/index.js +2 -2
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection.d.ts +58 -0
- package/dist/src/connection.d.ts.map +1 -0
- package/dist/src/connection.js +295 -0
- package/dist/src/connection.js.map +1 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +16 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/upgrader.d.ts +2 -10
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +15 -220
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +1 -1
- package/dist/src/version.js.map +1 -1
- package/package.json +11 -12
- package/src/address-manager/dns-mappings.ts +1 -1
- package/src/connection-manager/index.ts +2 -2
- package/src/connection.ts +395 -0
- package/src/index.ts +20 -0
- package/src/upgrader.ts +20 -281
- package/src/version.ts +1 -1
- package/dist/src/connection/index.d.ts +0 -84
- package/dist/src/connection/index.d.ts.map +0 -1
- package/dist/src/connection/index.js +0 -144
- package/dist/src/connection/index.js.map +0 -1
- package/dist/typedoc-urls.json +0 -22
- package/src/connection/index.ts +0 -199
package/src/connection/index.ts
DELETED
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
import { connectionSymbol, LimitedConnectionError, ConnectionClosedError, ConnectionClosingError } from '@libp2p/interface'
|
|
2
|
-
import { setMaxListeners } from 'main-event'
|
|
3
|
-
import type { AbortOptions, Logger, ComponentLogger, Direction, Connection, Stream, ConnectionTimeline, ConnectionStatus, NewStreamOptions, PeerId, ConnectionLimits } from '@libp2p/interface'
|
|
4
|
-
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
5
|
-
|
|
6
|
-
const CLOSE_TIMEOUT = 500
|
|
7
|
-
|
|
8
|
-
interface ConnectionInit {
|
|
9
|
-
remoteAddr: Multiaddr
|
|
10
|
-
remotePeer: PeerId
|
|
11
|
-
newStream(protocols: string[], options?: AbortOptions): Promise<Stream>
|
|
12
|
-
close(options?: AbortOptions): Promise<void>
|
|
13
|
-
abort(err: Error): void
|
|
14
|
-
getStreams(): Stream[]
|
|
15
|
-
status: ConnectionStatus
|
|
16
|
-
direction: Direction
|
|
17
|
-
timeline: ConnectionTimeline
|
|
18
|
-
multiplexer?: string
|
|
19
|
-
encryption?: string
|
|
20
|
-
limits?: ConnectionLimits
|
|
21
|
-
logger: ComponentLogger
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* An implementation of the js-libp2p connection.
|
|
26
|
-
* Any libp2p transport should use an upgrader to return this connection.
|
|
27
|
-
*/
|
|
28
|
-
export class ConnectionImpl implements Connection {
|
|
29
|
-
/**
|
|
30
|
-
* Connection identifier.
|
|
31
|
-
*/
|
|
32
|
-
public readonly id: string
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Observed multiaddr of the remote peer
|
|
36
|
-
*/
|
|
37
|
-
public readonly remoteAddr: Multiaddr
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Remote peer id
|
|
41
|
-
*/
|
|
42
|
-
public readonly remotePeer: PeerId
|
|
43
|
-
|
|
44
|
-
public direction: Direction
|
|
45
|
-
public timeline: ConnectionTimeline
|
|
46
|
-
public multiplexer?: string
|
|
47
|
-
public encryption?: string
|
|
48
|
-
public status: ConnectionStatus
|
|
49
|
-
public limits?: ConnectionLimits
|
|
50
|
-
public readonly log: Logger
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* User provided tags
|
|
54
|
-
*
|
|
55
|
-
*/
|
|
56
|
-
public tags: string[]
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Reference to the new stream function of the multiplexer
|
|
60
|
-
*/
|
|
61
|
-
private readonly _newStream: (protocols: string[], options?: NewStreamOptions) => Promise<Stream>
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Reference to the close function of the raw connection
|
|
65
|
-
*/
|
|
66
|
-
private readonly _close: (options?: AbortOptions) => Promise<void>
|
|
67
|
-
|
|
68
|
-
private readonly _abort: (err: Error) => void
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Reference to the getStreams function of the muxer
|
|
72
|
-
*/
|
|
73
|
-
private readonly _getStreams: () => Stream[]
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* An implementation of the js-libp2p connection.
|
|
77
|
-
* Any libp2p transport should use an upgrader to return this connection.
|
|
78
|
-
*/
|
|
79
|
-
constructor (init: ConnectionInit) {
|
|
80
|
-
const { remoteAddr, remotePeer, newStream, close, abort, getStreams } = init
|
|
81
|
-
|
|
82
|
-
this.id = `${(parseInt(String(Math.random() * 1e9))).toString(36)}${Date.now()}`
|
|
83
|
-
this.remoteAddr = remoteAddr
|
|
84
|
-
this.remotePeer = remotePeer
|
|
85
|
-
this.direction = init.direction
|
|
86
|
-
this.status = 'open'
|
|
87
|
-
this.timeline = init.timeline
|
|
88
|
-
this.multiplexer = init.multiplexer
|
|
89
|
-
this.encryption = init.encryption
|
|
90
|
-
this.limits = init.limits
|
|
91
|
-
this.log = init.logger.forComponent(`libp2p:connection:${this.direction}:${this.id}`)
|
|
92
|
-
|
|
93
|
-
if (this.remoteAddr.getPeerId() == null) {
|
|
94
|
-
this.remoteAddr = this.remoteAddr.encapsulate(`/p2p/${this.remotePeer}`)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
this._newStream = newStream
|
|
98
|
-
this._close = close
|
|
99
|
-
this._abort = abort
|
|
100
|
-
this._getStreams = getStreams
|
|
101
|
-
this.tags = []
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
readonly [Symbol.toStringTag] = 'Connection'
|
|
105
|
-
|
|
106
|
-
readonly [connectionSymbol] = true
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Get all the streams of the muxer
|
|
110
|
-
*/
|
|
111
|
-
get streams (): Stream[] {
|
|
112
|
-
return this._getStreams()
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Create a new stream from this connection
|
|
117
|
-
*/
|
|
118
|
-
async newStream (protocols: string | string[], options?: NewStreamOptions): Promise<Stream> {
|
|
119
|
-
if (this.status === 'closing') {
|
|
120
|
-
throw new ConnectionClosingError('the connection is being closed')
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (this.status === 'closed') {
|
|
124
|
-
throw new ConnectionClosedError('the connection is closed')
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (!Array.isArray(protocols)) {
|
|
128
|
-
protocols = [protocols]
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (this.limits != null && options?.runOnLimitedConnection !== true) {
|
|
132
|
-
throw new LimitedConnectionError('Cannot open protocol stream on limited connection')
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const stream = await this._newStream(protocols, options)
|
|
136
|
-
|
|
137
|
-
stream.direction = 'outbound'
|
|
138
|
-
|
|
139
|
-
return stream
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Close the connection
|
|
144
|
-
*/
|
|
145
|
-
async close (options: AbortOptions = {}): Promise<void> {
|
|
146
|
-
if (this.status === 'closed' || this.status === 'closing') {
|
|
147
|
-
return
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
this.log('closing connection to %a', this.remoteAddr)
|
|
151
|
-
|
|
152
|
-
this.status = 'closing'
|
|
153
|
-
|
|
154
|
-
if (options.signal == null) {
|
|
155
|
-
const signal = AbortSignal.timeout(CLOSE_TIMEOUT)
|
|
156
|
-
setMaxListeners(Infinity, signal)
|
|
157
|
-
|
|
158
|
-
options = {
|
|
159
|
-
...options,
|
|
160
|
-
signal
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
try {
|
|
165
|
-
this.log.trace('closing underlying transport')
|
|
166
|
-
|
|
167
|
-
// close raw connection
|
|
168
|
-
await this._close(options)
|
|
169
|
-
|
|
170
|
-
this.log.trace('updating timeline with close time')
|
|
171
|
-
|
|
172
|
-
this.status = 'closed'
|
|
173
|
-
this.timeline.close = Date.now()
|
|
174
|
-
} catch (err: any) {
|
|
175
|
-
this.log.error('error encountered during graceful close of connection to %a', this.remoteAddr, err)
|
|
176
|
-
this.abort(err)
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
abort (err: Error): void {
|
|
181
|
-
if (this.status === 'closed') {
|
|
182
|
-
return
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
this.log.error('aborting connection to %a due to error', this.remoteAddr, err)
|
|
186
|
-
|
|
187
|
-
this.status = 'closing'
|
|
188
|
-
|
|
189
|
-
// Abort raw connection
|
|
190
|
-
this._abort(err)
|
|
191
|
-
|
|
192
|
-
this.status = 'closed'
|
|
193
|
-
this.timeline.close = Date.now()
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export function createConnection (init: ConnectionInit): Connection {
|
|
198
|
-
return new ConnectionImpl(init)
|
|
199
|
-
}
|