@libp2p/interface-compliance-tests 6.1.8-665769021 → 6.1.8-7626b224d
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/matchers.d.ts +6 -0
- package/dist/src/matchers.d.ts.map +1 -1
- package/dist/src/matchers.js +6 -0
- package/dist/src/matchers.js.map +1 -1
- package/dist/src/mocks/muxer.d.ts +8 -3
- package/dist/src/mocks/muxer.d.ts.map +1 -1
- package/dist/src/mocks/muxer.js +15 -6
- package/dist/src/mocks/muxer.js.map +1 -1
- package/dist/src/mocks/upgrader.d.ts.map +1 -1
- package/dist/src/mocks/upgrader.js +0 -1
- package/dist/src/mocks/upgrader.js.map +1 -1
- package/dist/src/transport/index.d.ts +23 -10
- package/dist/src/transport/index.d.ts.map +1 -1
- package/dist/src/transport/index.js +217 -6
- package/dist/src/transport/index.js.map +1 -1
- package/dist/src/transport/utils.d.ts +17 -0
- package/dist/src/transport/utils.d.ts.map +1 -0
- package/dist/src/transport/utils.js +63 -0
- package/dist/src/transport/utils.js.map +1 -0
- package/package.json +15 -13
- package/src/matchers.ts +6 -0
- package/src/mocks/muxer.ts +24 -10
- package/src/mocks/upgrader.ts +1 -3
- package/src/transport/index.ts +314 -16
- package/src/transport/utils.ts +76 -0
- package/dist/src/connection/index.d.ts +0 -5
- package/dist/src/connection/index.d.ts.map +0 -1
- package/dist/src/connection/index.js +0 -135
- package/dist/src/connection/index.js.map +0 -1
- package/dist/src/transport/dial-test.d.ts +0 -5
- package/dist/src/transport/dial-test.d.ts.map +0 -1
- package/dist/src/transport/dial-test.js +0 -99
- package/dist/src/transport/dial-test.js.map +0 -1
- package/dist/src/transport/filter-test.d.ts +0 -5
- package/dist/src/transport/filter-test.d.ts.map +0 -1
- package/dist/src/transport/filter-test.js +0 -24
- package/dist/src/transport/filter-test.js.map +0 -1
- package/dist/src/transport/listen-test.d.ts +0 -5
- package/dist/src/transport/listen-test.d.ts.map +0 -1
- package/dist/src/transport/listen-test.js +0 -154
- package/dist/src/transport/listen-test.js.map +0 -1
- package/src/connection/index.ts +0 -166
- package/src/transport/dial-test.ts +0 -125
- package/src/transport/filter-test.ts +0 -32
- package/src/transport/listen-test.ts +0 -192
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/interface-compliance-tests",
|
|
3
|
-
"version": "6.1.8-
|
|
3
|
+
"version": "6.1.8-7626b224d",
|
|
4
4
|
"description": "Compliance tests for JS libp2p interfaces",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/interface-compliance-tests#readme",
|
|
@@ -48,10 +48,6 @@
|
|
|
48
48
|
"types": "./dist/src/index.d.ts",
|
|
49
49
|
"import": "./dist/src/index.js"
|
|
50
50
|
},
|
|
51
|
-
"./connection": {
|
|
52
|
-
"types": "./dist/src/connection/index.d.ts",
|
|
53
|
-
"import": "./dist/src/connection/index.js"
|
|
54
|
-
},
|
|
55
51
|
"./connection-encryption": {
|
|
56
52
|
"types": "./dist/src/connection-encryption/index.d.ts",
|
|
57
53
|
"import": "./dist/src/connection-encryption/index.js"
|
|
@@ -108,15 +104,19 @@
|
|
|
108
104
|
"test:electron-main": "aegir test -t electron-main"
|
|
109
105
|
},
|
|
110
106
|
"dependencies": {
|
|
111
|
-
"@libp2p/crypto": "5.0.6-
|
|
112
|
-
"@libp2p/
|
|
113
|
-
"@libp2p/interface
|
|
114
|
-
"@libp2p/
|
|
115
|
-
"@libp2p/
|
|
116
|
-
"@libp2p/
|
|
117
|
-
"@libp2p/
|
|
118
|
-
"@libp2p/
|
|
107
|
+
"@libp2p/crypto": "5.0.6-7626b224d",
|
|
108
|
+
"@libp2p/echo": "2.1.1-7626b224d",
|
|
109
|
+
"@libp2p/interface": "2.2.0-7626b224d",
|
|
110
|
+
"@libp2p/interface-internal": "2.0.10-7626b224d",
|
|
111
|
+
"@libp2p/logger": "5.1.3-7626b224d",
|
|
112
|
+
"@libp2p/memory": "0.0.0-7626b224d",
|
|
113
|
+
"@libp2p/multistream-select": "6.0.8-7626b224d",
|
|
114
|
+
"@libp2p/peer-collections": "6.0.10-7626b224d",
|
|
115
|
+
"@libp2p/peer-id": "5.0.7-7626b224d",
|
|
116
|
+
"@libp2p/plaintext": "2.0.10-7626b224d",
|
|
117
|
+
"@libp2p/utils": "6.1.3-7626b224d",
|
|
119
118
|
"@multiformats/multiaddr": "^12.2.3",
|
|
119
|
+
"@multiformats/multiaddr-matcher": "^1.5.0",
|
|
120
120
|
"abortable-iterator": "^5.0.1",
|
|
121
121
|
"aegir": "^44.0.1",
|
|
122
122
|
"delay": "^6.0.0",
|
|
@@ -131,12 +131,14 @@
|
|
|
131
131
|
"it-pushable": "^3.2.3",
|
|
132
132
|
"it-stream-types": "^2.0.1",
|
|
133
133
|
"it-to-buffer": "^4.0.7",
|
|
134
|
+
"libp2p": "2.2.1-7626b224d",
|
|
134
135
|
"merge-options": "^3.0.4",
|
|
135
136
|
"p-defer": "^4.0.1",
|
|
136
137
|
"p-event": "^6.0.1",
|
|
137
138
|
"p-limit": "^6.0.0",
|
|
138
139
|
"p-wait-for": "^5.0.2",
|
|
139
140
|
"protons-runtime": "^5.4.0",
|
|
141
|
+
"race-signal": "^1.1.0",
|
|
140
142
|
"sinon": "^18.0.0",
|
|
141
143
|
"tdigest": "^0.1.2",
|
|
142
144
|
"uint8arraylist": "^2.4.8",
|
package/src/matchers.ts
CHANGED
|
@@ -2,10 +2,16 @@ import Sinon from 'sinon'
|
|
|
2
2
|
import type { PeerId } from '@libp2p/interface'
|
|
3
3
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated PeerIds can be passed to sinon matchers directly
|
|
7
|
+
*/
|
|
5
8
|
export function matchPeerId (peerId: PeerId): Sinon.SinonMatcher {
|
|
6
9
|
return Sinon.match(p => p.toString() === peerId.toString())
|
|
7
10
|
}
|
|
8
11
|
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Multiaddrs can be passed to sinon matchers directly
|
|
14
|
+
*/
|
|
9
15
|
export function matchMultiaddr (ma: Multiaddr): Sinon.SinonMatcher {
|
|
10
16
|
return Sinon.match(m => m.toString() === ma.toString())
|
|
11
17
|
}
|
package/src/mocks/muxer.ts
CHANGED
|
@@ -27,9 +27,15 @@ interface ResetMessage {
|
|
|
27
27
|
direction: Direction
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
interface
|
|
30
|
+
interface CloseWriteMessage {
|
|
31
31
|
id: string
|
|
32
|
-
type: '
|
|
32
|
+
type: 'closeWrite'
|
|
33
|
+
direction: Direction
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface CloseReadMessage {
|
|
37
|
+
id: string
|
|
38
|
+
type: 'closeRead'
|
|
33
39
|
direction: Direction
|
|
34
40
|
}
|
|
35
41
|
|
|
@@ -39,7 +45,7 @@ interface CreateMessage {
|
|
|
39
45
|
direction: 'outbound'
|
|
40
46
|
}
|
|
41
47
|
|
|
42
|
-
type StreamMessage = DataMessage | ResetMessage |
|
|
48
|
+
type StreamMessage = DataMessage | ResetMessage | CloseWriteMessage | CloseReadMessage | CreateMessage
|
|
43
49
|
|
|
44
50
|
export interface MockMuxedStreamInit extends AbstractStreamInit {
|
|
45
51
|
push: Pushable<StreamMessage>
|
|
@@ -84,16 +90,21 @@ class MuxedStream extends AbstractStream {
|
|
|
84
90
|
}
|
|
85
91
|
|
|
86
92
|
sendCloseWrite (): void {
|
|
87
|
-
const closeMsg:
|
|
93
|
+
const closeMsg: CloseWriteMessage = {
|
|
88
94
|
id: this.id,
|
|
89
|
-
type: '
|
|
95
|
+
type: 'closeWrite',
|
|
90
96
|
direction: this.direction
|
|
91
97
|
}
|
|
92
98
|
this.push.push(closeMsg)
|
|
93
99
|
}
|
|
94
100
|
|
|
95
101
|
sendCloseRead (): void {
|
|
96
|
-
|
|
102
|
+
const closeMsg: CloseReadMessage = {
|
|
103
|
+
id: this.id,
|
|
104
|
+
type: 'closeRead',
|
|
105
|
+
direction: this.direction
|
|
106
|
+
}
|
|
107
|
+
this.push.push(closeMsg)
|
|
97
108
|
}
|
|
98
109
|
}
|
|
99
110
|
|
|
@@ -153,10 +164,10 @@ class MockMuxer implements StreamMuxer {
|
|
|
153
164
|
}
|
|
154
165
|
)
|
|
155
166
|
|
|
156
|
-
this.log('
|
|
167
|
+
this.log('muxer ended')
|
|
157
168
|
this.input.end()
|
|
158
169
|
} catch (err: any) {
|
|
159
|
-
this.log('
|
|
170
|
+
this.log.error('muxer errored - %e', err)
|
|
160
171
|
this.input.end(err)
|
|
161
172
|
}
|
|
162
173
|
}
|
|
@@ -192,9 +203,12 @@ class MockMuxer implements StreamMuxer {
|
|
|
192
203
|
} else if (message.type === 'reset') {
|
|
193
204
|
this.log('-> reset stream %s %s', muxedStream.direction, muxedStream.id)
|
|
194
205
|
muxedStream.reset()
|
|
195
|
-
} else if (message.type === '
|
|
196
|
-
this.log('-> closing stream %s %s', muxedStream.direction, muxedStream.id)
|
|
206
|
+
} else if (message.type === 'closeWrite') {
|
|
207
|
+
this.log('-> closing writeable end of stream %s %s', muxedStream.direction, muxedStream.id)
|
|
197
208
|
muxedStream.remoteCloseWrite()
|
|
209
|
+
} else if (message.type === 'closeRead') {
|
|
210
|
+
this.log('-> closing readable end of stream %s %s', muxedStream.direction, muxedStream.id)
|
|
211
|
+
muxedStream.remoteCloseRead()
|
|
198
212
|
}
|
|
199
213
|
}
|
|
200
214
|
|
package/src/mocks/upgrader.ts
CHANGED
|
@@ -28,7 +28,7 @@ class MockUpgrader implements Upgrader {
|
|
|
28
28
|
return connection
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
async upgradeInbound (multiaddrConnection: MultiaddrConnection, opts: UpgraderOptions = {}): Promise<
|
|
31
|
+
async upgradeInbound (multiaddrConnection: MultiaddrConnection, opts: UpgraderOptions = {}): Promise<void> {
|
|
32
32
|
const connection = mockConnection(multiaddrConnection, {
|
|
33
33
|
direction: 'inbound',
|
|
34
34
|
registrar: this.registrar,
|
|
@@ -36,8 +36,6 @@ class MockUpgrader implements Upgrader {
|
|
|
36
36
|
})
|
|
37
37
|
|
|
38
38
|
this.events?.safeDispatchEvent('connection:open', { detail: connection })
|
|
39
|
-
|
|
40
|
-
return connection
|
|
41
39
|
}
|
|
42
40
|
}
|
|
43
41
|
|
package/src/transport/index.ts
CHANGED
|
@@ -1,27 +1,325 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { stop } from '@libp2p/interface'
|
|
2
|
+
import { expect } from 'aegir/chai'
|
|
3
|
+
import delay from 'delay'
|
|
4
|
+
import pDefer from 'p-defer'
|
|
5
|
+
import { pEvent } from 'p-event'
|
|
6
|
+
import pWaitFor from 'p-wait-for'
|
|
7
|
+
import { raceSignal } from 'race-signal'
|
|
8
|
+
import { isValidTick } from '../is-valid-tick.js'
|
|
9
|
+
import { createPeer, getTransportManager, getUpgrader, slowNetwork } from './utils.js'
|
|
4
10
|
import type { TestSetup } from '../index.js'
|
|
5
|
-
import type {
|
|
11
|
+
import type { Echo } from '@libp2p/echo'
|
|
12
|
+
import type { Connection, Libp2p, Stream } from '@libp2p/interface'
|
|
6
13
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
14
|
+
import type { MultiaddrMatcher } from '@multiformats/multiaddr-matcher'
|
|
15
|
+
import type { Libp2pInit } from 'libp2p'
|
|
16
|
+
import type { DeferredPromise } from 'p-defer'
|
|
7
17
|
|
|
8
|
-
export interface
|
|
9
|
-
|
|
10
|
-
|
|
18
|
+
export interface TransportTestFixtures {
|
|
19
|
+
/**
|
|
20
|
+
* Addresses that will be used to dial listeners - both addresses must resolve
|
|
21
|
+
* to the same node
|
|
22
|
+
*/
|
|
23
|
+
dialAddrs?: [Multiaddr, Multiaddr]
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Filter out any addresses that cannot be dialed by the transport
|
|
27
|
+
*/
|
|
28
|
+
dialMultiaddrMatcher: MultiaddrMatcher
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Filter out any addresses that cannot be listened on by the transport
|
|
32
|
+
*/
|
|
33
|
+
listenMultiaddrMatcher: MultiaddrMatcher
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Config that creates a libp2p node that can dial a listener
|
|
37
|
+
*/
|
|
38
|
+
dialer: Libp2pInit
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Config that creates a libp2p node that can accept dials
|
|
42
|
+
*/
|
|
43
|
+
listener?: Libp2pInit
|
|
11
44
|
}
|
|
12
45
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
46
|
+
async function getSetup (common: TestSetup<TransportTestFixtures>): Promise<{ dialer: Libp2p<{ echo: Echo }>, listener?: Libp2p<{ echo: Echo }>, dialAddrs: Multiaddr[], dialMultiaddrMatcher: MultiaddrMatcher, listenMultiaddrMatcher: MultiaddrMatcher }> {
|
|
47
|
+
const setup = await common.setup()
|
|
48
|
+
const dialer = await createPeer(setup.dialer)
|
|
49
|
+
let listener
|
|
50
|
+
|
|
51
|
+
if (setup.listener != null) {
|
|
52
|
+
listener = await createPeer(setup.listener)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const dialAddrs = listener?.getMultiaddrs() ?? setup.dialAddrs
|
|
56
|
+
|
|
57
|
+
if (dialAddrs == null) {
|
|
58
|
+
throw new Error('Listener config or dial addresses must be specified')
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
dialer,
|
|
63
|
+
listener,
|
|
64
|
+
dialAddrs: dialAddrs.filter(ma => setup.dialMultiaddrMatcher.exactMatch(ma)),
|
|
65
|
+
dialMultiaddrMatcher: setup.dialMultiaddrMatcher,
|
|
66
|
+
listenMultiaddrMatcher: setup.listenMultiaddrMatcher
|
|
67
|
+
}
|
|
19
68
|
}
|
|
20
69
|
|
|
21
70
|
export default (common: TestSetup<TransportTestFixtures>): void => {
|
|
22
71
|
describe('interface-transport', () => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
72
|
+
let dialer: Libp2p<{ echo: Echo }>
|
|
73
|
+
let listener: Libp2p<{ echo: Echo }> | undefined
|
|
74
|
+
let dialAddrs: Multiaddr[]
|
|
75
|
+
let dialMultiaddrMatcher: MultiaddrMatcher
|
|
76
|
+
|
|
77
|
+
afterEach(async () => {
|
|
78
|
+
await stop(dialer, listener)
|
|
79
|
+
await common.teardown()
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('simple', async () => {
|
|
83
|
+
({ dialer, listener, dialAddrs } = await getSetup(common))
|
|
84
|
+
|
|
85
|
+
const input = Uint8Array.from([0, 1, 2, 3, 4])
|
|
86
|
+
const output = await dialer.services.echo.echo(dialAddrs[0], input, {
|
|
87
|
+
signal: AbortSignal.timeout(5000)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
expect(output).to.equalBytes(input)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('should listen on multiple addresses', async () => {
|
|
94
|
+
({ dialer, listener, dialAddrs } = await getSetup(common))
|
|
95
|
+
|
|
96
|
+
const input = Uint8Array.from([0, 1, 2, 3, 4])
|
|
97
|
+
|
|
98
|
+
await expect(dialer.services.echo.echo(dialAddrs[0], input, {
|
|
99
|
+
signal: AbortSignal.timeout(5000)
|
|
100
|
+
})).to.eventually.deep.equal(input)
|
|
101
|
+
|
|
102
|
+
await expect(dialer.services.echo.echo(dialAddrs[1], input, {
|
|
103
|
+
signal: AbortSignal.timeout(5000)
|
|
104
|
+
})).to.eventually.deep.equal(input)
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('can close connections', async () => {
|
|
108
|
+
({ dialer, listener, dialAddrs } = await getSetup(common))
|
|
109
|
+
|
|
110
|
+
const conn = await dialer.dial(dialAddrs[0], {
|
|
111
|
+
signal: AbortSignal.timeout(5000)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
await conn.close()
|
|
115
|
+
expect(isValidTick(conn.timeline.close)).to.equal(true)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('abort before dialing throws AbortError', async () => {
|
|
119
|
+
({ dialer, listener, dialAddrs } = await getSetup(common))
|
|
120
|
+
|
|
121
|
+
const controller = new AbortController()
|
|
122
|
+
controller.abort()
|
|
123
|
+
|
|
124
|
+
await expect(dialer.dial(dialAddrs[0], {
|
|
125
|
+
signal: controller.signal
|
|
126
|
+
})).to.eventually.be.rejected()
|
|
127
|
+
.with.property('name', 'AbortError')
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('abort while dialing throws AbortError', async () => {
|
|
131
|
+
({ dialer, listener, dialAddrs } = await getSetup(common))
|
|
132
|
+
slowNetwork(dialer, 100)
|
|
133
|
+
|
|
134
|
+
const controller = new AbortController()
|
|
135
|
+
setTimeout(() => { controller.abort() }, 50)
|
|
136
|
+
|
|
137
|
+
await expect(dialer.dial(dialAddrs[0], {
|
|
138
|
+
signal: controller.signal
|
|
139
|
+
})).to.eventually.be.rejected()
|
|
140
|
+
.with.property('name', 'AbortError')
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('should close all streams when the connection closes', async () => {
|
|
144
|
+
({ dialer, listener, dialAddrs } = await getSetup(common))
|
|
145
|
+
|
|
146
|
+
let incomingConnectionPromise: DeferredPromise<Connection> | undefined
|
|
147
|
+
|
|
148
|
+
if (listener != null) {
|
|
149
|
+
incomingConnectionPromise = pDefer<Connection>()
|
|
150
|
+
|
|
151
|
+
listener.addEventListener('connection:open', (event) => {
|
|
152
|
+
const conn = event.detail
|
|
153
|
+
|
|
154
|
+
if (conn.remotePeer.equals(dialer.peerId)) {
|
|
155
|
+
incomingConnectionPromise?.resolve(conn)
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const connection = await dialer.dial(dialAddrs[0])
|
|
161
|
+
let remoteConn: Connection | undefined
|
|
162
|
+
|
|
163
|
+
if (incomingConnectionPromise != null) {
|
|
164
|
+
remoteConn = await incomingConnectionPromise.promise
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const streams: Stream[] = []
|
|
168
|
+
|
|
169
|
+
for (let i = 0; i < 5; i++) {
|
|
170
|
+
streams.push(await connection.newStream('/echo/1.0.0', {
|
|
171
|
+
maxOutboundStreams: 5
|
|
172
|
+
}))
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Close the connection and verify all streams have been closed
|
|
176
|
+
await connection.close()
|
|
177
|
+
|
|
178
|
+
await pWaitFor(() => connection.streams.length === 0, {
|
|
179
|
+
timeout: 5000
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
if (remoteConn != null) {
|
|
183
|
+
await pWaitFor(() => remoteConn.streams.length === 0, {
|
|
184
|
+
timeout: 5000
|
|
185
|
+
})
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
expect(streams.find(stream => stream.status === 'open')).to.be.undefined()
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
it('should not handle connection if upgradeInbound rejects', async function () {
|
|
192
|
+
({ dialer, listener, dialAddrs, dialMultiaddrMatcher } = await getSetup(common))
|
|
193
|
+
|
|
194
|
+
if (listener == null) {
|
|
195
|
+
return this.skip()
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const upgrader = getUpgrader(listener)
|
|
199
|
+
upgrader.upgradeInbound = async () => {
|
|
200
|
+
await delay(100)
|
|
201
|
+
throw new Error('Oh noes!')
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
await expect(dialer.dial(dialAddrs[0])).to.eventually.be.rejected
|
|
205
|
+
.with.property('name', 'EncryptionFailedError')
|
|
206
|
+
|
|
207
|
+
expect(dialer.getConnections().filter(conn => {
|
|
208
|
+
return dialMultiaddrMatcher.exactMatch(conn.remoteAddr)
|
|
209
|
+
})).to.have.lengthOf(0)
|
|
210
|
+
|
|
211
|
+
if (listener != null) {
|
|
212
|
+
const remoteConnections = listener.getConnections(dialer.peerId)
|
|
213
|
+
.filter(conn => dialMultiaddrMatcher.exactMatch(conn.remoteAddr))
|
|
214
|
+
expect(remoteConnections).to.have.lengthOf(0)
|
|
215
|
+
}
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
it('should omit peerid in listening addresses', async function () {
|
|
219
|
+
({ dialer, listener, dialAddrs } = await getSetup(common))
|
|
220
|
+
|
|
221
|
+
if (listener == null) {
|
|
222
|
+
return this.skip()
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const tm = getTransportManager(listener)
|
|
226
|
+
const transportListeners = tm.getListeners()
|
|
227
|
+
|
|
228
|
+
for (const transportListener of transportListeners) {
|
|
229
|
+
for (const ma of transportListener.getAddrs()) {
|
|
230
|
+
expect(ma.toString()).to.not.include(`/p2p/${listener.peerId}`)
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
it('should handle one big write', async function () {
|
|
236
|
+
const timeout = 120_000
|
|
237
|
+
this.timeout(timeout);
|
|
238
|
+
({ dialer, listener, dialAddrs } = await getSetup(common))
|
|
239
|
+
|
|
240
|
+
const input = new Uint8Array(1024 * 1024 * 10).fill(5)
|
|
241
|
+
const output = await dialer.services.echo.echo(dialAddrs[0], input, {
|
|
242
|
+
signal: AbortSignal.timeout(timeout)
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
expect(output).to.equalBytes(input)
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
it('should handle many small writes', async function () {
|
|
249
|
+
const timeout = 120_000
|
|
250
|
+
this.timeout(timeout);
|
|
251
|
+
({ dialer, listener, dialAddrs } = await getSetup(common))
|
|
252
|
+
|
|
253
|
+
for (let i = 0; i < 2000; i++) {
|
|
254
|
+
const input = new Uint8Array(1024).fill(5)
|
|
255
|
+
const output = await dialer.services.echo.echo(dialAddrs[0], input, {
|
|
256
|
+
signal: AbortSignal.timeout(timeout)
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
expect(output).to.equalBytes(input)
|
|
260
|
+
}
|
|
261
|
+
})
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
describe('events', () => {
|
|
265
|
+
let dialer: Libp2p<{ echo: Echo }>
|
|
266
|
+
let listener: Libp2p<{ echo: Echo }> | undefined
|
|
267
|
+
let listenMultiaddrMatcher: MultiaddrMatcher
|
|
268
|
+
|
|
269
|
+
afterEach(async () => {
|
|
270
|
+
await stop(dialer, listener)
|
|
271
|
+
await common.teardown()
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
it('emits listening', async function () {
|
|
275
|
+
({ dialer, listener, listenMultiaddrMatcher } = await getSetup(common))
|
|
276
|
+
|
|
277
|
+
if (listener == null) {
|
|
278
|
+
return this.skip()
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
await listener.stop()
|
|
282
|
+
|
|
283
|
+
const transportListeningPromise = pDefer()
|
|
284
|
+
|
|
285
|
+
listener.addEventListener('transport:listening', (event) => {
|
|
286
|
+
const transportListener = event.detail
|
|
287
|
+
|
|
288
|
+
if (transportListener.getAddrs().some(ma => listenMultiaddrMatcher.exactMatch(ma))) {
|
|
289
|
+
transportListeningPromise.resolve()
|
|
290
|
+
}
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
await listener.start()
|
|
294
|
+
|
|
295
|
+
await raceSignal(transportListeningPromise.promise, AbortSignal.timeout(1000), {
|
|
296
|
+
errorMessage: 'Did not emit listening event'
|
|
297
|
+
})
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
it('emits close', async function () {
|
|
301
|
+
({ dialer, listener } = await getSetup(common))
|
|
302
|
+
|
|
303
|
+
if (listener == null) {
|
|
304
|
+
return this.skip()
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const transportManager = getTransportManager(listener)
|
|
308
|
+
const transportListener = transportManager.getListeners()
|
|
309
|
+
.filter(listener => listener.getAddrs().some(ma => listenMultiaddrMatcher.exactMatch(ma)))
|
|
310
|
+
.pop()
|
|
311
|
+
|
|
312
|
+
if (transportListener == null) {
|
|
313
|
+
throw new Error('Could not find address listener')
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const p = pEvent(transportListener, 'close')
|
|
317
|
+
|
|
318
|
+
await listener.stop()
|
|
319
|
+
|
|
320
|
+
await raceSignal(p, AbortSignal.timeout(1000), {
|
|
321
|
+
errorMessage: 'Did not emit close event'
|
|
322
|
+
})
|
|
323
|
+
})
|
|
26
324
|
})
|
|
27
325
|
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
import { echo } from '@libp2p/echo'
|
|
4
|
+
import { memory } from '@libp2p/memory'
|
|
5
|
+
import { plaintext } from '@libp2p/plaintext'
|
|
6
|
+
import delay from 'delay'
|
|
7
|
+
import map from 'it-map'
|
|
8
|
+
import { createLibp2p } from 'libp2p'
|
|
9
|
+
import { mockMuxer } from '../mocks/muxer.js'
|
|
10
|
+
import type { Echo } from '@libp2p/echo'
|
|
11
|
+
import type { Libp2p, Upgrader } from '@libp2p/interface'
|
|
12
|
+
import type { TransportManager } from '@libp2p/interface-internal'
|
|
13
|
+
import type { Libp2pOptions } from 'libp2p'
|
|
14
|
+
|
|
15
|
+
export async function createPeer (config: Partial<Libp2pOptions> = {}): Promise<Libp2p<{ echo: Echo }>> {
|
|
16
|
+
return createLibp2p({
|
|
17
|
+
transports: [
|
|
18
|
+
memory()
|
|
19
|
+
],
|
|
20
|
+
connectionEncrypters: [
|
|
21
|
+
plaintext()
|
|
22
|
+
],
|
|
23
|
+
streamMuxers: [
|
|
24
|
+
() => mockMuxer()
|
|
25
|
+
],
|
|
26
|
+
connectionGater: {
|
|
27
|
+
denyDialMultiaddr: () => false
|
|
28
|
+
},
|
|
29
|
+
...config,
|
|
30
|
+
services: {
|
|
31
|
+
...config.services,
|
|
32
|
+
echo: echo({
|
|
33
|
+
maxInboundStreams: 5
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Monkey patch the upgrader in the passed libp2p to add latency to any
|
|
41
|
+
* multiaddr connections upgraded to connections - this is to work with
|
|
42
|
+
* transports that have their own muxers/encrypters and do not support
|
|
43
|
+
* connection protection
|
|
44
|
+
*/
|
|
45
|
+
export function slowNetwork (libp2p: any, latency: number): void {
|
|
46
|
+
const upgrader: Upgrader = getUpgrader(libp2p)
|
|
47
|
+
|
|
48
|
+
const originalUpgradeInbound = upgrader.upgradeInbound.bind(upgrader)
|
|
49
|
+
const originalUpgradeOutbound = upgrader.upgradeOutbound.bind(upgrader)
|
|
50
|
+
|
|
51
|
+
upgrader.upgradeInbound = async (maConn, opts) => {
|
|
52
|
+
maConn.source = map(maConn.source, async (buf) => {
|
|
53
|
+
await delay(latency)
|
|
54
|
+
return buf
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
return originalUpgradeInbound(maConn, opts)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
upgrader.upgradeOutbound = async (maConn, opts) => {
|
|
61
|
+
maConn.source = map(maConn.source, async (buf) => {
|
|
62
|
+
await delay(latency)
|
|
63
|
+
return buf
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
return originalUpgradeOutbound(maConn, opts)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function getUpgrader (libp2p: any): Upgrader {
|
|
71
|
+
return libp2p.components.upgrader
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function getTransportManager (libp2p: any): TransportManager {
|
|
75
|
+
return libp2p.components.transportManager
|
|
76
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/connection/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;+BAE7B,UAAU,UAAU,CAAC,KAAG,IAAI;AAAlD,wBAgKC"}
|