This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.ts
131 lines (110 loc) · 3.35 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import type { EventEmitter, AbortOptions } from '../index.js'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { Connection } from '../connection/index.js'
import type { Duplex } from 'it-stream-types'
export const symbol = Symbol.for('@libp2p/transport')
export interface ConnectionHandler { (connection: Connection): void }
export interface MultiaddrFilter { (multiaddrs: Multiaddr[]): Multiaddr[] }
export interface CreateListenerOptions {
handler?: ConnectionHandler
upgrader: Upgrader
}
export interface DialOptions extends AbortOptions {
upgrader: Upgrader
}
/**
* A libp2p transport is understood as something that offers a dial and listen interface to establish connections.
*/
export interface Transport {
/**
* Used to identify the transport
*/
[Symbol.toStringTag]: string
/**
* Used by the isTransport function
*/
[symbol]: true
/**
* Dial a given multiaddr.
*/
dial: (ma: Multiaddr, options: DialOptions) => Promise<Connection>
/**
* Create transport listeners.
*/
createListener: (options: CreateListenerOptions) => Listener
/**
* Takes a list of `Multiaddr`s and returns only valid addresses for the transport
*/
filter: MultiaddrFilter
}
export interface ListenerEvents {
'connection': CustomEvent<Connection>
'listening': CustomEvent
'error': CustomEvent<Error>
'close': CustomEvent
}
export interface Listener extends EventEmitter<ListenerEvents> {
/**
* Start a listener
*/
listen: (multiaddr: Multiaddr) => Promise<void>
/**
* Get listen addresses
*/
getAddrs: () => Multiaddr[]
/**
* Close listener
*
* @returns {Promise<void>}
*/
close: () => Promise<void>
}
export interface UpgraderEvents {
'connection': CustomEvent<Connection>
'connectionEnd': CustomEvent<Connection>
}
export interface Upgrader extends EventEmitter<UpgraderEvents> {
/**
* Upgrades an outbound connection on `transport.dial`.
*/
upgradeOutbound: (maConn: MultiaddrConnection) => Promise<Connection>
/**
* Upgrades an inbound connection on transport listener.
*/
upgradeInbound: (maConn: MultiaddrConnection) => Promise<Connection>
}
export interface MultiaddrConnectionTimeline {
open: number
upgraded?: number
close?: number
}
/**
* A MultiaddrConnection is returned by transports after dialing
* a peer. It is a low-level primitive and is the raw connection
* without encryption or stream multiplexing.
*/
export interface MultiaddrConnection extends Duplex<Uint8Array> {
close: (err?: Error) => Promise<void>
remoteAddr: Multiaddr
timeline: MultiaddrConnectionTimeline
}
export interface ProtocolHandler {
(stream: Duplex<Uint8Array>, connection: Connection): void
}
export function isTransport (other: any): other is Transport {
return other != null && Boolean(other[symbol])
}
export interface TransportManagerEvents {
'listener:listening': CustomEvent<Listener>
'listener:close': CustomEvent<Listener>
}
export interface TransportManager extends EventEmitter<TransportManagerEvents> {
add: (transport: Transport) => void
dial: (ma: Multiaddr, options?: any) => Promise<Connection>
getAddrs: () => Multiaddr[]
getTransports: () => Transport[]
transportForMultiaddr: (ma: Multiaddr) => Transport | undefined
listen: (addrs: Multiaddr[]) => Promise<void>
remove: (key: string) => Promise<void>
removeAll: () => Promise<void>
}