@libp2p/interface 0.0.1-6fdaa7dc
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/LICENSE +4 -0
- package/README.md +45 -0
- package/dist/index.min.js +3 -0
- package/dist/src/connection/index.d.ts +188 -0
- package/dist/src/connection/index.d.ts.map +1 -0
- package/dist/src/connection/index.js +5 -0
- package/dist/src/connection/index.js.map +1 -0
- package/dist/src/connection/status.d.ts +4 -0
- package/dist/src/connection/status.d.ts.map +1 -0
- package/dist/src/connection/status.js +4 -0
- package/dist/src/connection/status.js.map +1 -0
- package/dist/src/connection-encrypter/index.d.ts +27 -0
- package/dist/src/connection-encrypter/index.d.ts.map +1 -0
- package/dist/src/connection-encrypter/index.js +2 -0
- package/dist/src/connection-encrypter/index.js.map +1 -0
- package/dist/src/connection-gater/index.d.ts +117 -0
- package/dist/src/connection-gater/index.d.ts.map +1 -0
- package/dist/src/connection-gater/index.js +2 -0
- package/dist/src/connection-gater/index.js.map +1 -0
- package/dist/src/content-routing/index.d.ts +79 -0
- package/dist/src/content-routing/index.d.ts.map +1 -0
- package/dist/src/content-routing/index.js +21 -0
- package/dist/src/content-routing/index.js.map +1 -0
- package/dist/src/errors.d.ts +33 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +51 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/events.d.ts +28 -0
- package/dist/src/events.d.ts.map +1 -0
- package/dist/src/events.js +70 -0
- package/dist/src/events.js.map +1 -0
- package/dist/src/index.d.ts +575 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +17 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/keychain/index.d.ts +154 -0
- package/dist/src/keychain/index.d.ts.map +1 -0
- package/dist/src/keychain/index.js +23 -0
- package/dist/src/keychain/index.js.map +1 -0
- package/dist/src/keys/index.d.ts +35 -0
- package/dist/src/keys/index.d.ts.map +1 -0
- package/dist/src/keys/index.js +4 -0
- package/dist/src/keys/index.js.map +1 -0
- package/dist/src/metrics/index.d.ts +165 -0
- package/dist/src/metrics/index.d.ts.map +1 -0
- package/dist/src/metrics/index.js +2 -0
- package/dist/src/metrics/index.js.map +1 -0
- package/dist/src/metrics/tracked-map.d.ts +17 -0
- package/dist/src/metrics/tracked-map.d.ts.map +1 -0
- package/dist/src/metrics/tracked-map.js +38 -0
- package/dist/src/metrics/tracked-map.js.map +1 -0
- package/dist/src/peer-discovery/index.d.ts +28 -0
- package/dist/src/peer-discovery/index.d.ts.map +1 -0
- package/dist/src/peer-discovery/index.js +21 -0
- package/dist/src/peer-discovery/index.js.map +1 -0
- package/dist/src/peer-id/index.d.ts +30 -0
- package/dist/src/peer-id/index.d.ts.map +1 -0
- package/dist/src/peer-id/index.js +5 -0
- package/dist/src/peer-id/index.js.map +1 -0
- package/dist/src/peer-info/index.d.ts +8 -0
- package/dist/src/peer-info/index.d.ts.map +1 -0
- package/dist/src/peer-info/index.js +2 -0
- package/dist/src/peer-info/index.js.map +1 -0
- package/dist/src/peer-routing/index.d.ts +51 -0
- package/dist/src/peer-routing/index.d.ts.map +1 -0
- package/dist/src/peer-routing/index.js +21 -0
- package/dist/src/peer-routing/index.js.map +1 -0
- package/dist/src/peer-store/index.d.ts +247 -0
- package/dist/src/peer-store/index.d.ts.map +1 -0
- package/dist/src/peer-store/index.js +2 -0
- package/dist/src/peer-store/index.js.map +1 -0
- package/dist/src/peer-store/tags.d.ts +2 -0
- package/dist/src/peer-store/tags.d.ts.map +1 -0
- package/dist/src/peer-store/tags.js +2 -0
- package/dist/src/peer-store/tags.js.map +1 -0
- package/dist/src/record/index.d.ts +33 -0
- package/dist/src/record/index.d.ts.map +1 -0
- package/dist/src/record/index.js +2 -0
- package/dist/src/record/index.js.map +1 -0
- package/dist/src/startable.d.ts +46 -0
- package/dist/src/startable.d.ts.map +1 -0
- package/dist/src/startable.js +46 -0
- package/dist/src/startable.js.map +1 -0
- package/dist/src/stream-handler/index.d.ts +23 -0
- package/dist/src/stream-handler/index.d.ts.map +1 -0
- package/dist/src/stream-handler/index.js +2 -0
- package/dist/src/stream-handler/index.js.map +1 -0
- package/dist/src/stream-muxer/index.d.ts +51 -0
- package/dist/src/stream-muxer/index.d.ts.map +1 -0
- package/dist/src/stream-muxer/index.js +2 -0
- package/dist/src/stream-muxer/index.js.map +1 -0
- package/dist/src/stream-muxer/stream.d.ts +85 -0
- package/dist/src/stream-muxer/stream.d.ts.map +1 -0
- package/dist/src/stream-muxer/stream.js +255 -0
- package/dist/src/stream-muxer/stream.js.map +1 -0
- package/dist/src/topology/index.d.ts +9 -0
- package/dist/src/topology/index.d.ts.map +1 -0
- package/dist/src/topology/index.js +2 -0
- package/dist/src/topology/index.js.map +1 -0
- package/dist/src/transport/index.d.ts +96 -0
- package/dist/src/transport/index.d.ts.map +1 -0
- package/dist/src/transport/index.js +19 -0
- package/dist/src/transport/index.js.map +1 -0
- package/package.json +177 -0
- package/src/connection/index.ts +221 -0
- package/src/connection/status.ts +4 -0
- package/src/connection-encrypter/index.ts +30 -0
- package/src/connection-gater/index.ts +128 -0
- package/src/content-routing/index.ts +83 -0
- package/src/errors.ts +68 -0
- package/src/events.ts +100 -0
- package/src/index.ts +627 -0
- package/src/keychain/index.ts +167 -0
- package/src/keys/index.ts +38 -0
- package/src/metrics/index.ts +187 -0
- package/src/metrics/tracked-map.ts +65 -0
- package/src/peer-discovery/index.ts +29 -0
- package/src/peer-id/index.ts +39 -0
- package/src/peer-info/index.ts +8 -0
- package/src/peer-routing/index.ts +53 -0
- package/src/peer-store/index.ts +272 -0
- package/src/peer-store/tags.ts +2 -0
- package/src/record/index.ts +35 -0
- package/src/startable.ts +117 -0
- package/src/stream-handler/index.ts +27 -0
- package/src/stream-muxer/index.ts +58 -0
- package/src/stream-muxer/stream.ts +365 -0
- package/src/topology/index.ts +10 -0
- package/src/transport/index.ts +111 -0
package/package.json
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@libp2p/interface",
|
|
3
|
+
"version": "0.0.1-6fdaa7dc",
|
|
4
|
+
"description": "The interface implemented by a libp2p node",
|
|
5
|
+
"license": "Apache-2.0 OR MIT",
|
|
6
|
+
"homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/interface#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/libp2p/js-libp2p.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/libp2p/js-libp2p/issues"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"interface",
|
|
16
|
+
"libp2p"
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"types": "./dist/src/index.d.ts",
|
|
20
|
+
"typesVersions": {
|
|
21
|
+
"*": {
|
|
22
|
+
"*": [
|
|
23
|
+
"*",
|
|
24
|
+
"dist/*",
|
|
25
|
+
"dist/src/*",
|
|
26
|
+
"dist/src/*/index"
|
|
27
|
+
],
|
|
28
|
+
"src/*": [
|
|
29
|
+
"*",
|
|
30
|
+
"dist/*",
|
|
31
|
+
"dist/src/*",
|
|
32
|
+
"dist/src/*/index"
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"src",
|
|
38
|
+
"dist",
|
|
39
|
+
"!dist/test",
|
|
40
|
+
"!**/*.tsbuildinfo"
|
|
41
|
+
],
|
|
42
|
+
"exports": {
|
|
43
|
+
".": {
|
|
44
|
+
"types": "./dist/src/index.d.ts",
|
|
45
|
+
"import": "./dist/src/index.js"
|
|
46
|
+
},
|
|
47
|
+
"./connection": {
|
|
48
|
+
"types": "./dist/src/connection/index.d.ts",
|
|
49
|
+
"import": "./dist/src/connection/index.js"
|
|
50
|
+
},
|
|
51
|
+
"./connection-encrypter": {
|
|
52
|
+
"types": "./dist/src/connection-encrypter/index.d.ts",
|
|
53
|
+
"import": "./dist/src/connection-encrypter/index.js"
|
|
54
|
+
},
|
|
55
|
+
"./connection-gater": {
|
|
56
|
+
"types": "./dist/src/connection-gater/index.d.ts",
|
|
57
|
+
"import": "./dist/src/connection-gater/index.js"
|
|
58
|
+
},
|
|
59
|
+
"./connection/status": {
|
|
60
|
+
"types": "./dist/src/connection/status.d.ts",
|
|
61
|
+
"import": "./dist/src/connection/status.js"
|
|
62
|
+
},
|
|
63
|
+
"./content-routing": {
|
|
64
|
+
"types": "./dist/src/content-routing/index.d.ts",
|
|
65
|
+
"import": "./dist/src/content-routing/index.js"
|
|
66
|
+
},
|
|
67
|
+
"./errors": {
|
|
68
|
+
"types": "./dist/src/errors.d.ts",
|
|
69
|
+
"import": "./dist/src/errors.js"
|
|
70
|
+
},
|
|
71
|
+
"./events": {
|
|
72
|
+
"types": "./dist/src/events.d.ts",
|
|
73
|
+
"import": "./dist/src/events.js"
|
|
74
|
+
},
|
|
75
|
+
"./keychain": {
|
|
76
|
+
"types": "./dist/src/keychain/index.d.ts",
|
|
77
|
+
"import": "./dist/src/keychain/index.js"
|
|
78
|
+
},
|
|
79
|
+
"./keys": {
|
|
80
|
+
"types": "./dist/src/keys/index.d.ts",
|
|
81
|
+
"import": "./dist/src/keys/index.js"
|
|
82
|
+
},
|
|
83
|
+
"./metrics": {
|
|
84
|
+
"types": "./dist/src/metrics/index.d.ts",
|
|
85
|
+
"import": "./dist/src/metrics/index.js"
|
|
86
|
+
},
|
|
87
|
+
"./metrics/tracked-map": {
|
|
88
|
+
"types": "./dist/src/metrics/tracked-map.d.ts",
|
|
89
|
+
"import": "./dist/src/metrics/tracked-map.js"
|
|
90
|
+
},
|
|
91
|
+
"./peer-discovery": {
|
|
92
|
+
"types": "./dist/src/peer-discovery/index.d.ts",
|
|
93
|
+
"import": "./dist/src/peer-discovery/index.js"
|
|
94
|
+
},
|
|
95
|
+
"./peer-id": {
|
|
96
|
+
"types": "./dist/src/peer-id/index.d.ts",
|
|
97
|
+
"import": "./dist/src/peer-id/index.js"
|
|
98
|
+
},
|
|
99
|
+
"./peer-info": {
|
|
100
|
+
"types": "./dist/src/peer-info/index.d.ts",
|
|
101
|
+
"import": "./dist/src/peer-info/index.js"
|
|
102
|
+
},
|
|
103
|
+
"./peer-routing": {
|
|
104
|
+
"types": "./dist/src/peer-routing/index.d.ts",
|
|
105
|
+
"import": "./dist/src/peer-routing/index.js"
|
|
106
|
+
},
|
|
107
|
+
"./peer-store": {
|
|
108
|
+
"types": "./dist/src/peer-store/index.d.ts",
|
|
109
|
+
"import": "./dist/src/peer-store/index.js"
|
|
110
|
+
},
|
|
111
|
+
"./peer-store/tags": {
|
|
112
|
+
"types": "./dist/src/peer-store/tags.d.ts",
|
|
113
|
+
"import": "./dist/src/peer-store/tags.js"
|
|
114
|
+
},
|
|
115
|
+
"./record": {
|
|
116
|
+
"types": "./dist/src/record/index.d.ts",
|
|
117
|
+
"import": "./dist/src/record/index.js"
|
|
118
|
+
},
|
|
119
|
+
"./startable": {
|
|
120
|
+
"types": "./dist/src/startable.d.ts",
|
|
121
|
+
"import": "./dist/src/startable.js"
|
|
122
|
+
},
|
|
123
|
+
"./stream-handler": {
|
|
124
|
+
"types": "./dist/src/stream-handler/index.d.ts",
|
|
125
|
+
"import": "./dist/src/stream-handler/index.js"
|
|
126
|
+
},
|
|
127
|
+
"./stream-muxer": {
|
|
128
|
+
"types": "./dist/src/stream-muxer/index.d.ts",
|
|
129
|
+
"import": "./dist/src/stream-muxer/index.js"
|
|
130
|
+
},
|
|
131
|
+
"./stream-muxer/stream": {
|
|
132
|
+
"types": "./dist/src/stream-muxer/stream.d.ts",
|
|
133
|
+
"import": "./dist/src/stream-muxer/stream.js"
|
|
134
|
+
},
|
|
135
|
+
"./transport": {
|
|
136
|
+
"types": "./dist/src/transport/index.d.ts",
|
|
137
|
+
"import": "./dist/src/transport/index.js"
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
"eslintConfig": {
|
|
141
|
+
"extends": "ipfs",
|
|
142
|
+
"parserOptions": {
|
|
143
|
+
"sourceType": "module"
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
"scripts": {
|
|
147
|
+
"clean": "aegir clean",
|
|
148
|
+
"lint": "aegir lint",
|
|
149
|
+
"dep-check": "aegir dep-check",
|
|
150
|
+
"build": "aegir build",
|
|
151
|
+
"test": "aegir test",
|
|
152
|
+
"test:chrome": "aegir test -t browser --cov",
|
|
153
|
+
"test:chrome-webworker": "aegir test -t webworker",
|
|
154
|
+
"test:firefox": "aegir test -t browser -- --browser firefox",
|
|
155
|
+
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
|
|
156
|
+
"test:node": "aegir test -t node --cov",
|
|
157
|
+
"test:electron-main": "aegir test -t electron-main"
|
|
158
|
+
},
|
|
159
|
+
"dependencies": {
|
|
160
|
+
"@multiformats/multiaddr": "^12.1.3",
|
|
161
|
+
"abortable-iterator": "^5.0.1",
|
|
162
|
+
"any-signal": "^4.1.1",
|
|
163
|
+
"it-pushable": "^3.1.3",
|
|
164
|
+
"it-stream-types": "^2.0.1",
|
|
165
|
+
"multiformats": "^12.0.1",
|
|
166
|
+
"uint8arraylist": "^2.4.3"
|
|
167
|
+
},
|
|
168
|
+
"devDependencies": {
|
|
169
|
+
"@types/sinon": "^10.0.15",
|
|
170
|
+
"aegir": "^39.0.10",
|
|
171
|
+
"sinon": "^15.1.2",
|
|
172
|
+
"sinon-ts": "^1.0.0"
|
|
173
|
+
},
|
|
174
|
+
"typedoc": {
|
|
175
|
+
"entryPoint": "./src/index.ts"
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import type * as Status from './status.js'
|
|
2
|
+
import type { AbortOptions } from '../index.js'
|
|
3
|
+
import type { PeerId } from '../peer-id/index.js'
|
|
4
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
5
|
+
import type { Duplex, Source } from 'it-stream-types'
|
|
6
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
7
|
+
|
|
8
|
+
export interface ConnectionTimeline {
|
|
9
|
+
open: number
|
|
10
|
+
upgraded?: number
|
|
11
|
+
close?: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Outbound conections are opened by the local node, inbound streams are opened by the remote
|
|
16
|
+
*/
|
|
17
|
+
export type Direction = 'inbound' | 'outbound'
|
|
18
|
+
|
|
19
|
+
export interface ConnectionStat {
|
|
20
|
+
/**
|
|
21
|
+
* Outbound conections are opened by the local node, inbound streams are opened by the remote
|
|
22
|
+
*/
|
|
23
|
+
direction: Direction
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Lifecycle times for the connection
|
|
27
|
+
*/
|
|
28
|
+
timeline: ConnectionTimeline
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Once a multiplexer has been negotiated for this stream, it will be set on the stat object
|
|
32
|
+
*/
|
|
33
|
+
multiplexer?: string
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Once a connection encrypter has been negotiated for this stream, it will be set on the stat object
|
|
37
|
+
*/
|
|
38
|
+
encryption?: string
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The current status of the connection
|
|
42
|
+
*/
|
|
43
|
+
status: keyof typeof Status
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface StreamTimeline {
|
|
47
|
+
/**
|
|
48
|
+
* A timestamp of when the stream was opened
|
|
49
|
+
*/
|
|
50
|
+
open: number
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A timestamp of when the stream was closed for both reading and writing
|
|
54
|
+
*/
|
|
55
|
+
close?: number
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A timestamp of when the stream was closed for reading
|
|
59
|
+
*/
|
|
60
|
+
closeRead?: number
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A timestamp of when the stream was closed for writing
|
|
64
|
+
*/
|
|
65
|
+
closeWrite?: number
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A timestamp of when the stream was reset
|
|
69
|
+
*/
|
|
70
|
+
reset?: number
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface StreamStat {
|
|
74
|
+
/**
|
|
75
|
+
* Outbound streams are opened by the local node, inbound streams are opened by the remote
|
|
76
|
+
*/
|
|
77
|
+
direction: Direction
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Lifecycle times for the stream
|
|
81
|
+
*/
|
|
82
|
+
timeline: StreamTimeline
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Once a protocol has been negotiated for this stream, it will be set on the stat object
|
|
86
|
+
*/
|
|
87
|
+
protocol?: string
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* A Stream is a data channel between two peers that
|
|
92
|
+
* can be written to and read from at both ends.
|
|
93
|
+
*
|
|
94
|
+
* It may be encrypted and multiplexed depending on the
|
|
95
|
+
* configuration of the nodes.
|
|
96
|
+
*/
|
|
97
|
+
export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>> {
|
|
98
|
+
/**
|
|
99
|
+
* Closes the stream for **reading** *and* **writing**.
|
|
100
|
+
*
|
|
101
|
+
* Any buffered data in the source can still be consumed and the stream will end normally.
|
|
102
|
+
*
|
|
103
|
+
* This will cause a `CLOSE` message to be sent to the remote, *unless* the sink has already ended.
|
|
104
|
+
*
|
|
105
|
+
* The sink and the source will return normally.
|
|
106
|
+
*/
|
|
107
|
+
close: () => void
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Closes the stream for **reading**. If iterating over the source of this stream in a `for await of` loop, it will return (exit the loop) after any buffered data has been consumed.
|
|
111
|
+
*
|
|
112
|
+
* This function is called automatically by the muxer when it receives a `CLOSE` message from the remote.
|
|
113
|
+
*
|
|
114
|
+
* The source will return normally, the sink will continue to consume.
|
|
115
|
+
*/
|
|
116
|
+
closeRead: () => void
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Closes the stream for **writing**. If iterating over the source of this stream in a `for await of` loop, it will return (exit the loop) after any buffered data has been consumed.
|
|
120
|
+
*
|
|
121
|
+
* The source will return normally, the sink will continue to consume.
|
|
122
|
+
*/
|
|
123
|
+
closeWrite: () => void
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Closes the stream for **reading** *and* **writing**. This should be called when a *local error* has occurred.
|
|
127
|
+
*
|
|
128
|
+
* Note, if called without an error any buffered data in the source can still be consumed and the stream will end normally.
|
|
129
|
+
*
|
|
130
|
+
* This will cause a `RESET` message to be sent to the remote, *unless* the sink has already ended.
|
|
131
|
+
*
|
|
132
|
+
* The sink will return and the source will throw if an error is passed or return normally if not.
|
|
133
|
+
*/
|
|
134
|
+
abort: (err: Error) => void
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Closes the stream *immediately* for **reading** *and* **writing**. This should be called when a *remote error* has occurred.
|
|
138
|
+
*
|
|
139
|
+
* This function is called automatically by the muxer when it receives a `RESET` message from the remote.
|
|
140
|
+
*
|
|
141
|
+
* The sink will return and the source will throw.
|
|
142
|
+
*/
|
|
143
|
+
reset: () => void
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Unique identifier for a stream. Identifiers are not unique across muxers.
|
|
147
|
+
*/
|
|
148
|
+
id: string
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Stats about this stream
|
|
152
|
+
*/
|
|
153
|
+
stat: StreamStat
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* User defined stream metadata
|
|
157
|
+
*/
|
|
158
|
+
metadata: Record<string, any>
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export interface NewStreamOptions extends AbortOptions {
|
|
162
|
+
/**
|
|
163
|
+
* If specified, and no handler has been registered with the registrar for the
|
|
164
|
+
* successfully negotiated protocol, use this as the max outbound stream limit
|
|
165
|
+
* for the protocol
|
|
166
|
+
*/
|
|
167
|
+
maxOutboundStreams?: number
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* A Connection is a high-level representation of a connection
|
|
172
|
+
* to a remote peer that may have been secured by encryption and
|
|
173
|
+
* multiplexed, depending on the configuration of the nodes
|
|
174
|
+
* between which the connection is made.
|
|
175
|
+
*/
|
|
176
|
+
export interface Connection {
|
|
177
|
+
id: string
|
|
178
|
+
stat: ConnectionStat
|
|
179
|
+
remoteAddr: Multiaddr
|
|
180
|
+
remotePeer: PeerId
|
|
181
|
+
tags: string[]
|
|
182
|
+
streams: Stream[]
|
|
183
|
+
|
|
184
|
+
newStream: (multicodecs: string | string[], options?: NewStreamOptions) => Promise<Stream>
|
|
185
|
+
addStream: (stream: Stream) => void
|
|
186
|
+
removeStream: (id: string) => void
|
|
187
|
+
close: () => Promise<void>
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export const symbol = Symbol.for('@libp2p/connection')
|
|
191
|
+
|
|
192
|
+
export function isConnection (other: any): other is Connection {
|
|
193
|
+
return other != null && Boolean(other[symbol])
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface ConnectionProtector {
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Takes a given Connection and creates a private encryption stream
|
|
200
|
+
* between its two peers from the PSK the Protector instance was
|
|
201
|
+
* created with.
|
|
202
|
+
*/
|
|
203
|
+
protect: (connection: MultiaddrConnection) => Promise<MultiaddrConnection>
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export interface MultiaddrConnectionTimeline {
|
|
207
|
+
open: number
|
|
208
|
+
upgraded?: number
|
|
209
|
+
close?: number
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* A MultiaddrConnection is returned by transports after dialing
|
|
214
|
+
* a peer. It is a low-level primitive and is the raw connection
|
|
215
|
+
* without encryption or stream multiplexing.
|
|
216
|
+
*/
|
|
217
|
+
export interface MultiaddrConnection extends Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>> {
|
|
218
|
+
close: (err?: Error) => Promise<void>
|
|
219
|
+
remoteAddr: Multiaddr
|
|
220
|
+
timeline: MultiaddrConnectionTimeline
|
|
221
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { PeerId } from '../peer-id/index.js'
|
|
2
|
+
import type { Duplex, Source } from 'it-stream-types'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A libp2p connection encrypter module must be compliant to this interface
|
|
6
|
+
* to ensure all exchanged data between two peers is encrypted.
|
|
7
|
+
*/
|
|
8
|
+
export interface ConnectionEncrypter<Extension = unknown> {
|
|
9
|
+
protocol: string
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Encrypt outgoing data to the remote party. If the remote PeerId is known,
|
|
13
|
+
* pass it for extra verification, otherwise it will be determined during
|
|
14
|
+
* the handshake.
|
|
15
|
+
*/
|
|
16
|
+
secureOutbound: (localPeer: PeerId, connection: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>, remotePeer?: PeerId) => Promise<SecuredConnection<Extension>>
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Decrypt incoming data. If the remote PeerId is known,
|
|
20
|
+
* pass it for extra verification, otherwise it will be determined during
|
|
21
|
+
* the handshake
|
|
22
|
+
*/
|
|
23
|
+
secureInbound: (localPeer: PeerId, connection: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>, remotePeer?: PeerId) => Promise<SecuredConnection<Extension>>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface SecuredConnection<Extension = unknown> {
|
|
27
|
+
conn: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>
|
|
28
|
+
remoteExtensions?: Extension
|
|
29
|
+
remotePeer: PeerId
|
|
30
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { MultiaddrConnection } from '../connection/index.js'
|
|
2
|
+
import type { PeerId } from '../peer-id/index.js'
|
|
3
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
4
|
+
|
|
5
|
+
export interface ConnectionGater {
|
|
6
|
+
/**
|
|
7
|
+
* denyDialPeer tests whether we're permitted to Dial the
|
|
8
|
+
* specified peer.
|
|
9
|
+
*
|
|
10
|
+
* This is called by the dialer.connectToPeer implementation before
|
|
11
|
+
* dialling a peer.
|
|
12
|
+
*
|
|
13
|
+
* Return true to prevent dialing the passed peer.
|
|
14
|
+
*/
|
|
15
|
+
denyDialPeer?: (peerId: PeerId) => Promise<boolean>
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* denyDialMultiaddr tests whether we're permitted to dial the specified
|
|
19
|
+
* multiaddr.
|
|
20
|
+
*
|
|
21
|
+
* This is called by the connection manager - if the peer id of the remote
|
|
22
|
+
* node is known it will be present in the multiaddr.
|
|
23
|
+
*
|
|
24
|
+
* Return true to prevent dialing the passed peer on the passed multiaddr.
|
|
25
|
+
*/
|
|
26
|
+
denyDialMultiaddr?: (multiaddr: Multiaddr) => Promise<boolean>
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* denyInboundConnection tests whether an incipient inbound connection is allowed.
|
|
30
|
+
*
|
|
31
|
+
* This is called by the upgrader, or by the transport directly (e.g. QUIC,
|
|
32
|
+
* Bluetooth), straight after it has accepted a connection from its socket.
|
|
33
|
+
*
|
|
34
|
+
* Return true to deny the incoming passed connection.
|
|
35
|
+
*/
|
|
36
|
+
denyInboundConnection?: (maConn: MultiaddrConnection) => Promise<boolean>
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* denyOutboundConnection tests whether an incipient outbound connection is allowed.
|
|
40
|
+
*
|
|
41
|
+
* This is called by the upgrader, or by the transport directly (e.g. QUIC,
|
|
42
|
+
* Bluetooth), straight after it has created a connection with its socket.
|
|
43
|
+
*
|
|
44
|
+
* Return true to deny the incoming passed connection.
|
|
45
|
+
*/
|
|
46
|
+
denyOutboundConnection?: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* denyInboundEncryptedConnection tests whether a given connection, now encrypted,
|
|
50
|
+
* is allowed.
|
|
51
|
+
*
|
|
52
|
+
* This is called by the upgrader, after it has performed the security
|
|
53
|
+
* handshake, and before it negotiates the muxer, or by the directly by the
|
|
54
|
+
* transport, at the exact same checkpoint.
|
|
55
|
+
*
|
|
56
|
+
* Return true to deny the passed secured connection.
|
|
57
|
+
*/
|
|
58
|
+
denyInboundEncryptedConnection?: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* denyOutboundEncryptedConnection tests whether a given connection, now encrypted,
|
|
62
|
+
* is allowed.
|
|
63
|
+
*
|
|
64
|
+
* This is called by the upgrader, after it has performed the security
|
|
65
|
+
* handshake, and before it negotiates the muxer, or by the directly by the
|
|
66
|
+
* transport, at the exact same checkpoint.
|
|
67
|
+
*
|
|
68
|
+
* Return true to deny the passed secured connection.
|
|
69
|
+
*/
|
|
70
|
+
denyOutboundEncryptedConnection?: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* denyInboundUpgradedConnection tests whether a fully capable connection is allowed.
|
|
74
|
+
*
|
|
75
|
+
* This is called after encryption has been negotiated and the connection has been
|
|
76
|
+
* multiplexed, if a multiplexer is configured.
|
|
77
|
+
*
|
|
78
|
+
* Return true to deny the passed upgraded connection.
|
|
79
|
+
*/
|
|
80
|
+
denyInboundUpgradedConnection?: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* denyOutboundUpgradedConnection tests whether a fully capable connection is allowed.
|
|
84
|
+
*
|
|
85
|
+
* This is called after encryption has been negotiated and the connection has been
|
|
86
|
+
* multiplexed, if a multiplexer is configured.
|
|
87
|
+
*
|
|
88
|
+
* Return true to deny the passed upgraded connection.
|
|
89
|
+
*/
|
|
90
|
+
denyOutboundUpgradedConnection?: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* denyInboundRelayReservation tests whether a remote peer is allowed make a
|
|
94
|
+
* relay reservation on this node.
|
|
95
|
+
*
|
|
96
|
+
* Return true to deny the relay reservation.
|
|
97
|
+
*/
|
|
98
|
+
denyInboundRelayReservation?: (source: PeerId) => Promise<boolean>
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* denyOutboundRelayedConnection tests whether a remote peer is allowed to open a relayed
|
|
102
|
+
* connection to the destination node.
|
|
103
|
+
*
|
|
104
|
+
* This is invoked on the relay server when a source client with a reservation instructs
|
|
105
|
+
* the server to relay a connection to a destination peer.
|
|
106
|
+
*
|
|
107
|
+
* Return true to deny the relayed connection.
|
|
108
|
+
*/
|
|
109
|
+
denyOutboundRelayedConnection?: (source: PeerId, destination: PeerId) => Promise<boolean>
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* denyInboundRelayedConnection tests whether a remote peer is allowed to open a relayed
|
|
113
|
+
* connection to this node.
|
|
114
|
+
*
|
|
115
|
+
* This is invoked on the relay client when a remote relay has received an instruction to
|
|
116
|
+
* relay a connection to the client.
|
|
117
|
+
*
|
|
118
|
+
* Return true to deny the relayed connection.
|
|
119
|
+
*/
|
|
120
|
+
denyInboundRelayedConnection?: (relay: PeerId, remotePeer: PeerId) => Promise<boolean>
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Used by the address book to filter passed addresses.
|
|
124
|
+
*
|
|
125
|
+
* Return true to allow storing the passed multiaddr for the passed peer.
|
|
126
|
+
*/
|
|
127
|
+
filterMultiaddrForPeer?: (peer: PeerId, multiaddr: Multiaddr) => Promise<boolean>
|
|
128
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { AbortOptions } from '../index.js'
|
|
2
|
+
import type { PeerInfo } from '../peer-info/index.js'
|
|
3
|
+
import type { CID } from 'multiformats/cid'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Any object that implements this Symbol as a property should return a
|
|
7
|
+
* ContentRouting instance as the property value, similar to how
|
|
8
|
+
* `Symbol.Iterable` can be used to return an `Iterable` from an `Iterator`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```js
|
|
13
|
+
* import { contentRouting, ContentRouting } from '@libp2p/content-routing'
|
|
14
|
+
*
|
|
15
|
+
* class MyContentRouter implements ContentRouting {
|
|
16
|
+
* get [contentRouting] () {
|
|
17
|
+
* return this
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // ...other methods
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export const contentRouting = Symbol.for('@libp2p/content-routing')
|
|
25
|
+
|
|
26
|
+
export interface ContentRouting {
|
|
27
|
+
/**
|
|
28
|
+
* The implementation of this method should ensure that network peers know the
|
|
29
|
+
* caller can provide content that corresponds to the passed CID.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
*
|
|
33
|
+
* ```js
|
|
34
|
+
* // ...
|
|
35
|
+
* await contentRouting.provide(cid)
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
provide: (cid: CID, options?: AbortOptions) => Promise<void>
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Find the providers of the passed CID.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
*
|
|
45
|
+
* ```js
|
|
46
|
+
* // Iterate over the providers found for the given cid
|
|
47
|
+
* for await (const provider of contentRouting.findProviders(cid)) {
|
|
48
|
+
* console.log(provider.id, provider.multiaddrs)
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
findProviders: (cid: CID, options?: AbortOptions) => AsyncIterable<PeerInfo>
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Puts a value corresponding to the passed key in a way that can later be
|
|
56
|
+
* retrieved by another network peer using the get method.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
*
|
|
60
|
+
* ```js
|
|
61
|
+
* // ...
|
|
62
|
+
* const key = '/key'
|
|
63
|
+
* const value = uint8ArrayFromString('oh hello there')
|
|
64
|
+
*
|
|
65
|
+
* await contentRouting.put(key, value)
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
put: (key: Uint8Array, value: Uint8Array, options?: AbortOptions) => Promise<void>
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Retrieves a value from the network corresponding to the passed key.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
*
|
|
75
|
+
* ```js
|
|
76
|
+
* // ...
|
|
77
|
+
*
|
|
78
|
+
* const key = '/key'
|
|
79
|
+
* const value = await contentRouting.get(key)
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
get: (key: Uint8Array, options?: AbortOptions) => Promise<Uint8Array>
|
|
83
|
+
}
|