libp2p 0.37.3-5371729 → 0.37.3-b1b2b21
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 +1 -0
- package/dist/src/circuit/circuit/hop.js +2 -2
- package/dist/src/circuit/circuit/hop.js.map +1 -1
- package/dist/src/circuit/circuit/stop.js +1 -1
- package/dist/src/circuit/circuit/stop.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +11 -3
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +0 -10
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +34 -39
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/errors.d.ts +4 -1
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +3 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/fetch/index.d.ts +3 -0
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +61 -39
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/identify/index.d.ts +4 -0
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +8 -3
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +4 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/libp2p.d.ts +3 -3
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +2 -2
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/ping/index.d.ts +3 -0
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +6 -2
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/registrar.d.ts +2 -0
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +6 -6
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +59 -8
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +20 -19
- package/src/circuit/circuit/hop.ts +2 -2
- package/src/circuit/circuit/stop.ts +1 -1
- package/src/config.ts +11 -3
- package/src/connection-manager/index.ts +42 -50
- package/src/errors.ts +4 -1
- package/src/fetch/index.ts +87 -46
- package/src/identify/index.ts +14 -3
- package/src/index.ts +4 -4
- package/src/libp2p.ts +3 -3
- package/src/ping/index.ts +9 -2
- package/src/registrar.ts +6 -6
- package/src/upgrader.ts +77 -11
package/src/index.ts
CHANGED
|
@@ -11,14 +11,14 @@ import type { PeerStore, PeerStoreInit } from '@libp2p/interface-peer-store'
|
|
|
11
11
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
12
12
|
import type { AutoRelayConfig, RelayAdvertiseConfig } from './circuit/index.js'
|
|
13
13
|
import type { PeerDiscovery } from '@libp2p/interface-peer-discovery'
|
|
14
|
-
import type { Connection, ConnectionGater, ConnectionProtector,
|
|
14
|
+
import type { Connection, ConnectionGater, ConnectionProtector, Stream } from '@libp2p/interface-connection'
|
|
15
15
|
import type { Transport } from '@libp2p/interface-transport'
|
|
16
16
|
import type { StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
|
|
17
17
|
import type { ConnectionEncrypter } from '@libp2p/interface-connection-encrypter'
|
|
18
18
|
import type { PeerRouting } from '@libp2p/interface-peer-routing'
|
|
19
19
|
import type { ContentRouting } from '@libp2p/interface-content-routing'
|
|
20
20
|
import type { PubSub } from '@libp2p/interface-pubsub'
|
|
21
|
-
import type { Registrar, StreamHandler } from '@libp2p/interface-registrar'
|
|
21
|
+
import type { Registrar, StreamHandler, StreamHandlerOptions } from '@libp2p/interface-registrar'
|
|
22
22
|
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
23
23
|
import type { Metrics, MetricsInit } from '@libp2p/interface-metrics'
|
|
24
24
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
@@ -177,7 +177,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
|
|
|
177
177
|
* If successful, the known metadata of the peer will be added to the nodes `peerStore`,
|
|
178
178
|
* and the `MuxedStream` will be returned together with the successful negotiated protocol.
|
|
179
179
|
*/
|
|
180
|
-
dialProtocol: (peer: PeerId | Multiaddr, protocols: string | string[], options?: AbortOptions) => Promise<
|
|
180
|
+
dialProtocol: (peer: PeerId | Multiaddr, protocols: string | string[], options?: AbortOptions) => Promise<Stream>
|
|
181
181
|
|
|
182
182
|
/**
|
|
183
183
|
* Disconnects all connections to the given `peer`
|
|
@@ -187,7 +187,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
|
|
|
187
187
|
/**
|
|
188
188
|
* Registers the `handler` for each protocol
|
|
189
189
|
*/
|
|
190
|
-
handle: (protocol: string | string[], handler: StreamHandler) => Promise<void>
|
|
190
|
+
handle: (protocol: string | string[], handler: StreamHandler, options?: StreamHandlerOptions) => Promise<void>
|
|
191
191
|
|
|
192
192
|
/**
|
|
193
193
|
* Removes the handler for each protocol. The protocol
|
package/src/libp2p.ts
CHANGED
|
@@ -33,7 +33,7 @@ import type { Connection } from '@libp2p/interface-connection'
|
|
|
33
33
|
import type { PeerRouting } from '@libp2p/interface-peer-routing'
|
|
34
34
|
import type { ContentRouting } from '@libp2p/interface-content-routing'
|
|
35
35
|
import type { PubSub } from '@libp2p/interface-pubsub'
|
|
36
|
-
import type { Registrar, StreamHandler } from '@libp2p/interface-registrar'
|
|
36
|
+
import type { Registrar, StreamHandler, StreamHandlerOptions } from '@libp2p/interface-registrar'
|
|
37
37
|
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
38
38
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
39
39
|
import type { Libp2p, Libp2pEvents, Libp2pInit, Libp2pOptions } from './index.js'
|
|
@@ -490,14 +490,14 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
|
|
|
490
490
|
return await this.pingService.ping(id, options)
|
|
491
491
|
}
|
|
492
492
|
|
|
493
|
-
async handle (protocols: string | string[], handler: StreamHandler): Promise<void> {
|
|
493
|
+
async handle (protocols: string | string[], handler: StreamHandler, options?: StreamHandlerOptions): Promise<void> {
|
|
494
494
|
if (!Array.isArray(protocols)) {
|
|
495
495
|
protocols = [protocols]
|
|
496
496
|
}
|
|
497
497
|
|
|
498
498
|
await Promise.all(
|
|
499
499
|
protocols.map(async protocol => {
|
|
500
|
-
await this.components.getRegistrar().handle(protocol, handler)
|
|
500
|
+
await this.components.getRegistrar().handle(protocol, handler, options)
|
|
501
501
|
})
|
|
502
502
|
)
|
|
503
503
|
}
|
package/src/ping/index.ts
CHANGED
|
@@ -18,21 +18,28 @@ const log = logger('libp2p:ping')
|
|
|
18
18
|
|
|
19
19
|
export interface PingServiceInit {
|
|
20
20
|
protocolPrefix: string
|
|
21
|
+
maxInboundStreams: number
|
|
22
|
+
maxOutboundStreams: number
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
export class PingService implements Startable {
|
|
24
26
|
public readonly protocol: string
|
|
25
27
|
private readonly components: Components
|
|
26
28
|
private started: boolean
|
|
29
|
+
private readonly init: PingServiceInit
|
|
27
30
|
|
|
28
31
|
constructor (components: Components, init: PingServiceInit) {
|
|
29
32
|
this.components = components
|
|
30
33
|
this.started = false
|
|
31
34
|
this.protocol = `/${init.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
|
|
35
|
+
this.init = init
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
async start () {
|
|
35
|
-
await this.components.getRegistrar().handle(this.protocol, this.handleMessage
|
|
39
|
+
await this.components.getRegistrar().handle(this.protocol, this.handleMessage, {
|
|
40
|
+
maxInboundStreams: this.init.maxInboundStreams,
|
|
41
|
+
maxOutboundStreams: this.init.maxOutboundStreams
|
|
42
|
+
})
|
|
36
43
|
this.started = true
|
|
37
44
|
}
|
|
38
45
|
|
|
@@ -67,7 +74,7 @@ export class PingService implements Startable {
|
|
|
67
74
|
log('dialing %s to %p', this.protocol, peer)
|
|
68
75
|
|
|
69
76
|
const connection = await this.components.getConnectionManager().openConnection(peer, options)
|
|
70
|
-
const
|
|
77
|
+
const stream = await connection.newStream([this.protocol], options)
|
|
71
78
|
const start = Date.now()
|
|
72
79
|
const data = randomBytes(PING_LENGTH)
|
|
73
80
|
|
package/src/registrar.ts
CHANGED
|
@@ -10,8 +10,8 @@ import type { Components } from '@libp2p/components'
|
|
|
10
10
|
|
|
11
11
|
const log = logger('libp2p:registrar')
|
|
12
12
|
|
|
13
|
-
const
|
|
14
|
-
const
|
|
13
|
+
export const DEFAULT_MAX_INBOUND_STREAMS = 1
|
|
14
|
+
export const DEFAULT_MAX_OUTBOUND_STREAMS = 1
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Responsible for notifying registered protocols of events in the network.
|
|
@@ -46,7 +46,7 @@ export class DefaultRegistrar implements Registrar {
|
|
|
46
46
|
const handler = this.handlers.get(protocol)
|
|
47
47
|
|
|
48
48
|
if (handler == null) {
|
|
49
|
-
throw new Error(`No handler registered for protocol ${protocol}`)
|
|
49
|
+
throw errCode(new Error(`No handler registered for protocol ${protocol}`), codes.ERR_NO_HANDLER_FOR_PROTOCOL)
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
return handler
|
|
@@ -72,9 +72,9 @@ export class DefaultRegistrar implements Registrar {
|
|
|
72
72
|
throw errCode(new Error(`Handler already registered for protocol ${protocol}`), codes.ERR_PROTOCOL_HANDLER_ALREADY_REGISTERED)
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
const options = merge({
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
const options = merge.bind({ ignoreUndefined: true })({
|
|
76
|
+
maxInboundStreams: DEFAULT_MAX_INBOUND_STREAMS,
|
|
77
|
+
maxOutboundStreams: DEFAULT_MAX_OUTBOUND_STREAMS
|
|
78
78
|
}, opts)
|
|
79
79
|
|
|
80
80
|
this.handlers.set(protocol, {
|
package/src/upgrader.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { codes } from './errors.js'
|
|
|
8
8
|
import { createConnection } from '@libp2p/connection'
|
|
9
9
|
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
10
10
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
11
|
-
import type { MultiaddrConnection, Connection,
|
|
11
|
+
import type { MultiaddrConnection, Connection, Stream } from '@libp2p/interface-connection'
|
|
12
12
|
import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter'
|
|
13
13
|
import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
|
|
14
14
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
@@ -16,6 +16,8 @@ import type { Upgrader, UpgraderEvents } from '@libp2p/interface-transport'
|
|
|
16
16
|
import type { Duplex } from 'it-stream-types'
|
|
17
17
|
import { Components, isInitializable } from '@libp2p/components'
|
|
18
18
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
19
|
+
import type { Registrar } from '@libp2p/interface-registrar'
|
|
20
|
+
import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js'
|
|
19
21
|
|
|
20
22
|
const log = logger('libp2p:upgrader')
|
|
21
23
|
|
|
@@ -43,6 +45,46 @@ export interface UpgraderInit {
|
|
|
43
45
|
muxers: StreamMuxerFactory[]
|
|
44
46
|
}
|
|
45
47
|
|
|
48
|
+
function findIncomingStreamLimit (protocol: string, registrar: Registrar) {
|
|
49
|
+
try {
|
|
50
|
+
const { options } = registrar.getHandler(protocol)
|
|
51
|
+
|
|
52
|
+
return options.maxInboundStreams
|
|
53
|
+
} catch (err: any) {
|
|
54
|
+
if (err.code !== codes.ERR_NO_HANDLER_FOR_PROTOCOL) {
|
|
55
|
+
throw err
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return DEFAULT_MAX_INBOUND_STREAMS
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function findOutgoingStreamLimit (protocol: string, registrar: Registrar) {
|
|
63
|
+
try {
|
|
64
|
+
const { options } = registrar.getHandler(protocol)
|
|
65
|
+
|
|
66
|
+
return options.maxOutboundStreams
|
|
67
|
+
} catch (err: any) {
|
|
68
|
+
if (err.code !== codes.ERR_NO_HANDLER_FOR_PROTOCOL) {
|
|
69
|
+
throw err
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return DEFAULT_MAX_OUTBOUND_STREAMS
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function countStreams (protocol: string, direction: 'inbound' | 'outbound', connection: Connection) {
|
|
77
|
+
let streamCount = 0
|
|
78
|
+
|
|
79
|
+
connection.streams.forEach(stream => {
|
|
80
|
+
if (stream.stat.direction === direction && stream.stat.protocol === protocol) {
|
|
81
|
+
streamCount++
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
return streamCount
|
|
86
|
+
}
|
|
87
|
+
|
|
46
88
|
export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upgrader {
|
|
47
89
|
private readonly components: Components
|
|
48
90
|
private readonly connectionEncryption: Map<string, ConnectionEncrypter>
|
|
@@ -267,7 +309,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
267
309
|
} = opts
|
|
268
310
|
|
|
269
311
|
let muxer: StreamMuxer | undefined
|
|
270
|
-
let newStream: ((multicodecs: string[], options?: AbortOptions) => Promise<
|
|
312
|
+
let newStream: ((multicodecs: string[], options?: AbortOptions) => Promise<Stream>) | undefined
|
|
271
313
|
let connection: Connection // eslint-disable-line prefer-const
|
|
272
314
|
|
|
273
315
|
if (muxerFactory != null) {
|
|
@@ -296,13 +338,22 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
296
338
|
return
|
|
297
339
|
}
|
|
298
340
|
|
|
299
|
-
|
|
341
|
+
const incomingLimit = findIncomingStreamLimit(protocol, this.components.getRegistrar())
|
|
342
|
+
const streamCount = countStreams(protocol, 'inbound', connection)
|
|
343
|
+
|
|
344
|
+
if (streamCount === incomingLimit) {
|
|
345
|
+
throw errCode(new Error('Too many incoming protocol streams'), codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
muxedStream.stat.protocol = protocol
|
|
349
|
+
|
|
350
|
+
connection.addStream(muxedStream)
|
|
300
351
|
this._onStream({ connection, stream: { ...muxedStream, ...stream }, protocol })
|
|
301
352
|
})
|
|
302
353
|
.catch(err => {
|
|
303
354
|
log.error(err)
|
|
304
355
|
|
|
305
|
-
if (muxedStream.timeline.close == null) {
|
|
356
|
+
if (muxedStream.stat.timeline.close == null) {
|
|
306
357
|
muxedStream.close()
|
|
307
358
|
}
|
|
308
359
|
})
|
|
@@ -317,7 +368,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
317
368
|
muxer.init(this.components)
|
|
318
369
|
}
|
|
319
370
|
|
|
320
|
-
newStream = async (protocols: string[], options: AbortOptions = {}): Promise<
|
|
371
|
+
newStream = async (protocols: string[], options: AbortOptions = {}): Promise<Stream> => {
|
|
321
372
|
if (muxer == null) {
|
|
322
373
|
throw errCode(new Error('Stream is not multiplexed'), codes.ERR_MUXER_UNAVAILABLE)
|
|
323
374
|
}
|
|
@@ -334,11 +385,27 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
334
385
|
stream = metrics.trackStream({ stream, remotePeer, protocol })
|
|
335
386
|
}
|
|
336
387
|
|
|
337
|
-
|
|
388
|
+
const outgoingLimit = findOutgoingStreamLimit(protocol, this.components.getRegistrar())
|
|
389
|
+
const streamCount = countStreams(protocol, 'outbound', connection)
|
|
390
|
+
|
|
391
|
+
if (streamCount === outgoingLimit) {
|
|
392
|
+
throw errCode(new Error('Too many outgoing protocol streams'), codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
muxedStream.stat.protocol = protocol
|
|
396
|
+
|
|
397
|
+
return {
|
|
398
|
+
...muxedStream,
|
|
399
|
+
...stream,
|
|
400
|
+
stat: {
|
|
401
|
+
...muxedStream.stat,
|
|
402
|
+
protocol
|
|
403
|
+
}
|
|
404
|
+
}
|
|
338
405
|
} catch (err: any) {
|
|
339
406
|
log.error('could not create new stream', err)
|
|
340
407
|
|
|
341
|
-
if (muxedStream.timeline.close == null) {
|
|
408
|
+
if (muxedStream.stat.timeline.close == null) {
|
|
342
409
|
muxedStream.close()
|
|
343
410
|
}
|
|
344
411
|
|
|
@@ -402,9 +469,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
402
469
|
await maConn.close()
|
|
403
470
|
// Ensure remaining streams are closed
|
|
404
471
|
if (muxer != null) {
|
|
405
|
-
|
|
406
|
-
await stream.close()
|
|
407
|
-
}))
|
|
472
|
+
muxer.streams.forEach(s => s.close())
|
|
408
473
|
}
|
|
409
474
|
}
|
|
410
475
|
})
|
|
@@ -422,7 +487,8 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
422
487
|
_onStream (opts: OnStreamOptions): void {
|
|
423
488
|
const { connection, stream, protocol } = opts
|
|
424
489
|
const { handler } = this.components.getRegistrar().getHandler(protocol)
|
|
425
|
-
|
|
490
|
+
|
|
491
|
+
handler({ connection, stream })
|
|
426
492
|
}
|
|
427
493
|
|
|
428
494
|
/**
|