@libp2p/interface-compliance-tests 6.1.7 → 6.1.8-32ca76fcb

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.
Files changed (42) hide show
  1. package/dist/src/matchers.d.ts +6 -0
  2. package/dist/src/matchers.d.ts.map +1 -1
  3. package/dist/src/matchers.js +6 -0
  4. package/dist/src/matchers.js.map +1 -1
  5. package/dist/src/mocks/muxer.d.ts +8 -3
  6. package/dist/src/mocks/muxer.d.ts.map +1 -1
  7. package/dist/src/mocks/muxer.js +15 -6
  8. package/dist/src/mocks/muxer.js.map +1 -1
  9. package/dist/src/transport/index.d.ts +13 -9
  10. package/dist/src/transport/index.d.ts.map +1 -1
  11. package/dist/src/transport/index.js +321 -6
  12. package/dist/src/transport/index.js.map +1 -1
  13. package/dist/src/transport/utils.d.ts +19 -0
  14. package/dist/src/transport/utils.d.ts.map +1 -0
  15. package/dist/src/transport/utils.js +62 -0
  16. package/dist/src/transport/utils.js.map +1 -0
  17. package/package.json +14 -13
  18. package/src/matchers.ts +6 -0
  19. package/src/mocks/muxer.ts +24 -10
  20. package/src/transport/index.ts +405 -17
  21. package/src/transport/utils.ts +76 -0
  22. package/dist/src/connection/index.d.ts +0 -5
  23. package/dist/src/connection/index.d.ts.map +0 -1
  24. package/dist/src/connection/index.js +0 -135
  25. package/dist/src/connection/index.js.map +0 -1
  26. package/dist/src/transport/dial-test.d.ts +0 -5
  27. package/dist/src/transport/dial-test.d.ts.map +0 -1
  28. package/dist/src/transport/dial-test.js +0 -99
  29. package/dist/src/transport/dial-test.js.map +0 -1
  30. package/dist/src/transport/filter-test.d.ts +0 -5
  31. package/dist/src/transport/filter-test.d.ts.map +0 -1
  32. package/dist/src/transport/filter-test.js +0 -24
  33. package/dist/src/transport/filter-test.js.map +0 -1
  34. package/dist/src/transport/listen-test.d.ts +0 -5
  35. package/dist/src/transport/listen-test.d.ts.map +0 -1
  36. package/dist/src/transport/listen-test.js +0 -154
  37. package/dist/src/transport/listen-test.js.map +0 -1
  38. package/dist/typedoc-urls.json +0 -49
  39. package/src/connection/index.ts +0 -166
  40. package/src/transport/dial-test.ts +0 -125
  41. package/src/transport/filter-test.ts +0 -32
  42. package/src/transport/listen-test.ts +0 -192
@@ -1,166 +0,0 @@
1
- import { expect } from 'aegir/chai'
2
- import sinon from 'sinon'
3
- import type { TestSetup } from '../index.js'
4
- import type { Connection } from '@libp2p/interface'
5
-
6
- export default (test: TestSetup<Connection>): void => {
7
- describe('connection', () => {
8
- describe('open connection', () => {
9
- let connection: Connection
10
-
11
- beforeEach(async () => {
12
- connection = await test.setup()
13
- })
14
-
15
- afterEach(async () => {
16
- await connection.close()
17
- await test.teardown()
18
- })
19
-
20
- it('should have properties set', () => {
21
- expect(connection.id).to.exist()
22
- expect(connection.remotePeer).to.exist()
23
- expect(connection.remoteAddr).to.exist()
24
- expect(connection.status).to.equal('open')
25
- expect(connection.timeline.open).to.exist()
26
- expect(connection.timeline.close).to.not.exist()
27
- expect(connection.direction).to.exist()
28
- expect(connection.streams).to.eql([])
29
- expect(connection.tags).to.eql([])
30
- })
31
-
32
- it('should get the metadata of an open connection', () => {
33
- expect(connection.status).to.equal('open')
34
- expect(connection.direction).to.exist()
35
- expect(connection.timeline.open).to.exist()
36
- expect(connection.timeline.close).to.not.exist()
37
- })
38
-
39
- it('should return an empty array of streams', () => {
40
- const streams = connection.streams
41
-
42
- expect(streams).to.eql([])
43
- })
44
-
45
- it('should be able to create a new stream', async () => {
46
- expect(connection.streams).to.be.empty()
47
-
48
- const protocolToUse = '/echo/0.0.1'
49
- const stream = await connection.newStream([protocolToUse])
50
-
51
- expect(stream).to.have.property('protocol', protocolToUse)
52
-
53
- const connStreams = connection.streams
54
-
55
- expect(stream).to.exist()
56
- expect(connStreams).to.exist()
57
- expect(connStreams).to.have.lengthOf(1)
58
- expect(connStreams[0]).to.equal(stream)
59
- })
60
- })
61
-
62
- describe('close connection', () => {
63
- let connection: Connection
64
- let timelineProxy
65
- const proxyHandler = {
66
- set () {
67
- // @ts-expect-error - TS fails to infer here
68
- return Reflect.set(...arguments)
69
- }
70
- }
71
-
72
- beforeEach(async () => {
73
- timelineProxy = new Proxy({
74
- open: Date.now() - 10,
75
- upgraded: Date.now()
76
- }, proxyHandler)
77
-
78
- connection = await test.setup()
79
- connection.timeline = timelineProxy
80
- })
81
-
82
- afterEach(async () => {
83
- await test.teardown()
84
- })
85
-
86
- it('should be able to close the connection after being created', async () => {
87
- expect(connection.timeline.close).to.not.exist()
88
- await connection.close()
89
-
90
- expect(connection.timeline.close).to.exist()
91
- expect(connection.status).to.equal('closed')
92
- })
93
-
94
- it('should be able to close the connection after opening a stream', async () => {
95
- // Open stream
96
- const protocol = '/echo/0.0.1'
97
- await connection.newStream([protocol])
98
-
99
- // Close connection
100
- expect(connection.timeline.close).to.not.exist()
101
- await connection.close()
102
-
103
- expect(connection.timeline.close).to.exist()
104
- expect(connection.status).to.equal('closed')
105
- })
106
-
107
- it('should properly track streams', async () => {
108
- // Open stream
109
- const protocol = '/echo/0.0.1'
110
- const stream = await connection.newStream([protocol])
111
- expect(stream).to.have.property('protocol', protocol)
112
-
113
- // Close stream
114
- await stream.close()
115
-
116
- expect(connection.streams.filter(s => s.id === stream.id)).to.be.empty()
117
- })
118
-
119
- it('should track outbound streams', async () => {
120
- // Open stream
121
- const protocol = '/echo/0.0.1'
122
- const stream = await connection.newStream(protocol)
123
- expect(stream).to.have.property('direction', 'outbound')
124
- })
125
-
126
- it('should support a proxy on the timeline', async () => {
127
- sinon.spy(proxyHandler, 'set')
128
- expect(connection.timeline.close).to.not.exist()
129
-
130
- await connection.close()
131
- // @ts-expect-error - fails to infer callCount
132
- expect(proxyHandler.set.callCount).to.equal(1)
133
- // @ts-expect-error - fails to infer getCall
134
- const [obj, key, value] = proxyHandler.set.getCall(0).args
135
- expect(obj).to.eql(connection.timeline)
136
- expect(key).to.equal('close')
137
- expect(value).to.be.a('number').that.equals(connection.timeline.close)
138
- })
139
-
140
- it('should fail to create a new stream if the connection is closing', async () => {
141
- expect(connection.timeline.close).to.not.exist()
142
- const p = connection.close()
143
-
144
- try {
145
- const protocol = '/echo/0.0.1'
146
- await connection.newStream([protocol])
147
- } catch (err: any) {
148
- expect(err).to.exist()
149
- return
150
- } finally {
151
- await p
152
- }
153
-
154
- throw new Error('should fail to create a new stream if the connection is closing')
155
- })
156
-
157
- it('should fail to create a new stream if the connection is closed', async () => {
158
- expect(connection.timeline.close).to.not.exist()
159
- await connection.close()
160
-
161
- await expect(connection.newStream(['/echo/0.0.1'])).to.eventually.be.rejected
162
- .with.property('name', 'ConnectionClosedError')
163
- })
164
- })
165
- })
166
- }
@@ -1,125 +0,0 @@
1
- import { TypedEventEmitter } from '@libp2p/interface'
2
- import { expect } from 'aegir/chai'
3
- import all from 'it-all'
4
- import drain from 'it-drain'
5
- import { pipe } from 'it-pipe'
6
- import sinon from 'sinon'
7
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8
- import { isValidTick } from '../is-valid-tick.js'
9
- import { mockUpgrader, mockRegistrar } from '../mocks/index.js'
10
- import type { TransportTestFixtures, Connector } from './index.js'
11
- import type { TestSetup } from '../index.js'
12
- import type { Listener, Transport, Upgrader } from '@libp2p/interface'
13
- import type { Registrar } from '@libp2p/interface-internal'
14
- import type { Multiaddr } from '@multiformats/multiaddr'
15
-
16
- export default (common: TestSetup<TransportTestFixtures>): void => {
17
- describe('dial', () => {
18
- let upgrader: Upgrader
19
- let registrar: Registrar
20
- let listenAddrs: Multiaddr[]
21
- let dialAddrs: Multiaddr[]
22
- let dialer: Transport
23
- let listener: Transport
24
- let connector: Connector
25
- let listen: Listener
26
-
27
- before(async () => {
28
- registrar = mockRegistrar()
29
- upgrader = mockUpgrader({
30
- registrar,
31
- events: new TypedEventEmitter()
32
- });
33
-
34
- ({ listenAddrs, dialAddrs, dialer, listener, connector } = await common.setup())
35
- })
36
-
37
- after(async () => {
38
- await common.teardown()
39
- })
40
-
41
- beforeEach(async () => {
42
- listen = listener.createListener({
43
- upgrader
44
- })
45
- await listen.listen(listenAddrs[0])
46
- })
47
-
48
- afterEach(async () => {
49
- sinon.restore()
50
- connector.restore()
51
- await listen.close()
52
- })
53
-
54
- it('simple', async () => {
55
- const protocol = '/hello/1.0.0'
56
- void registrar.handle(protocol, (data) => {
57
- void pipe([
58
- uint8ArrayFromString('hey')
59
- ],
60
- data.stream,
61
- drain
62
- )
63
- })
64
-
65
- const upgradeSpy = sinon.spy(upgrader, 'upgradeOutbound')
66
- const conn = await dialer.dial(dialAddrs[0], {
67
- upgrader
68
- })
69
-
70
- const stream = await conn.newStream([protocol])
71
- const result = await all(stream.source)
72
-
73
- expect(upgradeSpy.callCount).to.equal(1)
74
- await expect(upgradeSpy.getCall(0).returnValue).to.eventually.equal(conn)
75
- expect(result.length).to.equal(1)
76
- expect(result[0].subarray()).to.equalBytes(uint8ArrayFromString('hey'))
77
- await conn.close()
78
- })
79
-
80
- it('can close connections', async () => {
81
- const upgradeSpy = sinon.spy(upgrader, 'upgradeOutbound')
82
- const conn = await dialer.dial(dialAddrs[0], {
83
- upgrader
84
- })
85
-
86
- expect(upgradeSpy.callCount).to.equal(1)
87
- await expect(upgradeSpy.getCall(0).returnValue).to.eventually.equal(conn)
88
- await conn.close()
89
- expect(isValidTick(conn.timeline.close)).to.equal(true)
90
- })
91
-
92
- it('to non existent listener', async () => {
93
- const upgradeSpy = sinon.spy(upgrader, 'upgradeOutbound')
94
-
95
- await expect(dialer.dial(dialAddrs[1], {
96
- upgrader
97
- })).to.eventually.be.rejected()
98
- expect(upgradeSpy.callCount).to.equal(0)
99
- })
100
-
101
- it('abort before dialing throws AbortError', async () => {
102
- const upgradeSpy = sinon.spy(upgrader, 'upgradeOutbound')
103
- const controller = new AbortController()
104
- controller.abort()
105
- const conn = dialer.dial(dialAddrs[0], { signal: controller.signal, upgrader })
106
-
107
- await expect(conn).to.eventually.be.rejected().with.property('name', 'AbortError')
108
- expect(upgradeSpy.callCount).to.equal(0)
109
- })
110
-
111
- it('abort while dialing throws AbortError', async () => {
112
- const upgradeSpy = sinon.spy(upgrader, 'upgradeOutbound')
113
- // Add a delay to connect() so that we can abort while the dial is in
114
- // progress
115
- connector.delay(100)
116
-
117
- const controller = new AbortController()
118
- const conn = dialer.dial(dialAddrs[0], { signal: controller.signal, upgrader })
119
- setTimeout(() => { controller.abort() }, 50)
120
-
121
- await expect(conn).to.eventually.be.rejected().with.property('name', 'AbortError')
122
- expect(upgradeSpy.callCount).to.equal(0)
123
- })
124
- })
125
- }
@@ -1,32 +0,0 @@
1
- import { expect } from 'aegir/chai'
2
- import type { TransportTestFixtures } from './index.js'
3
- import type { TestSetup } from '../index.js'
4
- import type { Transport } from '@libp2p/interface'
5
- import type { Multiaddr } from '@multiformats/multiaddr'
6
-
7
- export default (common: TestSetup<TransportTestFixtures>): void => {
8
- describe('filter', () => {
9
- let listenAddrs: Multiaddr[]
10
- let dialAddrs: Multiaddr[]
11
- let dialer: Transport
12
- let listener: Transport
13
-
14
- before(async () => {
15
- ({ listenAddrs, dialAddrs, dialer, listener } = await common.setup())
16
- })
17
-
18
- after(async () => {
19
- await common.teardown()
20
- })
21
-
22
- it('filters listen addresses', () => {
23
- const filteredAddrs = listener.listenFilter(listenAddrs)
24
- expect(filteredAddrs).to.eql(listenAddrs)
25
- })
26
-
27
- it('filters dial addresses', () => {
28
- const filteredAddrs = dialer.dialFilter(dialAddrs)
29
- expect(filteredAddrs).to.eql(dialAddrs)
30
- })
31
- })
32
- }
@@ -1,192 +0,0 @@
1
- /* eslint max-nested-callbacks: ["error", 8] */
2
- import { TypedEventEmitter } from '@libp2p/interface'
3
- import { expect } from 'aegir/chai'
4
- import drain from 'it-drain'
5
- import { pipe } from 'it-pipe'
6
- import defer from 'p-defer'
7
- import pWaitFor from 'p-wait-for'
8
- import sinon from 'sinon'
9
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
10
- import { isValidTick } from '../is-valid-tick.js'
11
- import { mockUpgrader, mockRegistrar } from '../mocks/index.js'
12
- import type { TransportTestFixtures } from './index.js'
13
- import type { TestSetup } from '../index.js'
14
- import type { Connection, Transport, Upgrader } from '@libp2p/interface'
15
- import type { Registrar } from '@libp2p/interface-internal'
16
- import type { Multiaddr } from '@multiformats/multiaddr'
17
-
18
- export default (common: TestSetup<TransportTestFixtures>): void => {
19
- describe('listen', () => {
20
- let upgrader: Upgrader
21
- let listenAddrs: Multiaddr[]
22
- let dialAddrs: Multiaddr[]
23
- let dialer: Transport
24
- let listener: Transport
25
- let registrar: Registrar
26
-
27
- before(async () => {
28
- registrar = mockRegistrar()
29
- upgrader = mockUpgrader({
30
- registrar,
31
- events: new TypedEventEmitter()
32
- });
33
-
34
- ({ dialer, listener, listenAddrs, dialAddrs } = await common.setup())
35
- })
36
-
37
- after(async () => {
38
- await common.teardown()
39
- })
40
-
41
- afterEach(() => {
42
- sinon.restore()
43
- })
44
-
45
- it('simple', async () => {
46
- const listen = listener.createListener({
47
- upgrader
48
- })
49
- await listen.listen(listenAddrs[0])
50
- await listen.close()
51
- })
52
-
53
- it('close listener with connections, through timeout', async () => {
54
- const upgradeSpy = sinon.spy(upgrader, 'upgradeInbound')
55
- const listenerConns: Connection[] = []
56
-
57
- const protocol = '/test/protocol'
58
- void registrar.handle(protocol, (data) => {
59
- void drain(data.stream.source)
60
- })
61
-
62
- const listen = listener.createListener({
63
- upgrader,
64
- handler: (conn) => {
65
- listenerConns.push(conn)
66
- }
67
- })
68
-
69
- // Listen
70
- await listen.listen(listenAddrs[0])
71
-
72
- // Create two connections to the listener
73
- const [conn1] = await Promise.all([
74
- dialer.dial(dialAddrs[0], {
75
- upgrader
76
- }),
77
- dialer.dial(dialAddrs[0], {
78
- upgrader
79
- })
80
- ])
81
-
82
- // Give the listener a chance to finish its upgrade
83
- await pWaitFor(() => listenerConns.length === 2)
84
-
85
- const stream1 = await conn1.newStream([protocol])
86
-
87
- // Wait for the data send and close to finish
88
- await Promise.all([
89
- pipe(
90
- [uint8ArrayFromString('Some data that is never handled')],
91
- stream1
92
- ),
93
- // Closer the listener (will take a couple of seconds to time out)
94
- listen.close()
95
- ])
96
-
97
- await stream1.close()
98
- await conn1.close()
99
-
100
- expect(isValidTick(conn1.timeline.close)).to.equal(true)
101
- listenerConns.forEach(conn => {
102
- expect(isValidTick(conn.timeline.close)).to.equal(true)
103
- })
104
-
105
- // 2 dials = 2 connections upgraded
106
- expect(upgradeSpy.callCount).to.equal(2)
107
- })
108
-
109
- it('should not handle connection if upgradeInbound rejects', async () => {
110
- sinon.stub(upgrader, 'upgradeInbound').rejects()
111
-
112
- const listen = listener.createListener({
113
- upgrader
114
- })
115
-
116
- // Listen
117
- await listen.listen(listenAddrs[0])
118
-
119
- // Create a connection to the listener
120
- const conn = await dialer.dial(dialAddrs[0], {
121
- upgrader
122
- })
123
-
124
- await pWaitFor(() => typeof conn.timeline.close === 'number')
125
- await listen.close()
126
- })
127
-
128
- describe('events', () => {
129
- it('connection', async () => {
130
- const upgradeSpy = sinon.spy(upgrader, 'upgradeInbound')
131
- const listen = listener.createListener({
132
- upgrader
133
- })
134
- const deferred = defer()
135
- let conn
136
-
137
- listen.addEventListener('connection', (evt) => {
138
- conn = evt.detail
139
- deferred.resolve()
140
- })
141
-
142
- void (async () => {
143
- await listen.listen(listenAddrs[0])
144
- await dialer.dial(dialAddrs[0], {
145
- upgrader
146
- })
147
- })()
148
-
149
- await deferred.promise
150
-
151
- await expect(upgradeSpy.getCall(0).returnValue).to.eventually.equal(conn)
152
- expect(upgradeSpy.callCount).to.equal(1)
153
- await listen.close()
154
- })
155
-
156
- it('listening', (done) => {
157
- const listen = listener.createListener({
158
- upgrader
159
- })
160
- listen.addEventListener('listening', () => {
161
- listen.close().then(done, done)
162
- })
163
- void listen.listen(listenAddrs[0])
164
- })
165
-
166
- it('error', (done) => {
167
- const listen = listener.createListener({
168
- upgrader
169
- })
170
- listen.addEventListener('error', (evt) => {
171
- expect(evt.detail).to.be.an.instanceOf(Error)
172
- listen.close().then(done, done)
173
- })
174
- listen.dispatchEvent(new CustomEvent('error', {
175
- detail: new Error('my err')
176
- }))
177
- })
178
-
179
- it('close', (done) => {
180
- const listen = listener.createListener({
181
- upgrader
182
- })
183
- listen.addEventListener('close', () => { done() })
184
-
185
- void (async () => {
186
- await listen.listen(listenAddrs[0])
187
- await listen.close()
188
- })()
189
- })
190
- })
191
- })
192
- }