@libp2p/interface-compliance-tests 3.0.7-ab0e3980 → 3.0.7-b1024c6c
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 +9 -0
- package/dist/src/connection/index.d.ts +5 -0
- package/dist/src/connection/index.d.ts.map +1 -0
- package/dist/src/connection/index.js +150 -0
- package/dist/src/connection/index.js.map +1 -0
- package/dist/src/connection-encryption/index.d.ts +5 -0
- package/dist/src/connection-encryption/index.d.ts.map +1 -0
- package/dist/src/connection-encryption/index.js +71 -0
- package/dist/src/connection-encryption/index.js.map +1 -0
- package/dist/src/connection-encryption/utils/index.d.ts +3 -0
- package/dist/src/connection-encryption/utils/index.d.ts.map +1 -0
- package/dist/src/connection-encryption/utils/index.js +19 -0
- package/dist/src/connection-encryption/utils/index.js.map +1 -0
- package/dist/src/mocks/connection-encrypter.d.ts +3 -0
- package/dist/src/mocks/connection-encrypter.d.ts.map +1 -0
- package/dist/src/mocks/connection-encrypter.js +98 -0
- package/dist/src/mocks/connection-encrypter.js.map +1 -0
- package/dist/src/mocks/connection-gater.d.ts +3 -0
- package/dist/src/mocks/connection-gater.d.ts.map +1 -0
- package/dist/src/mocks/connection-gater.js +17 -0
- package/dist/src/mocks/connection-gater.js.map +1 -0
- package/dist/src/mocks/connection-manager.d.ts +29 -0
- package/dist/src/mocks/connection-manager.d.ts.map +1 -0
- package/dist/src/mocks/connection-manager.js +145 -0
- package/dist/src/mocks/connection-manager.js.map +1 -0
- package/dist/src/mocks/connection.d.ts +32 -0
- package/dist/src/mocks/connection.d.ts.map +1 -0
- package/dist/src/mocks/connection.js +165 -0
- package/dist/src/mocks/connection.js.map +1 -0
- package/dist/src/mocks/duplex.d.ts +3 -0
- package/dist/src/mocks/duplex.d.ts.map +1 -0
- package/dist/src/mocks/duplex.js +9 -0
- package/dist/src/mocks/duplex.js.map +1 -0
- package/dist/src/mocks/index.d.ts +13 -0
- package/dist/src/mocks/index.d.ts.map +1 -0
- package/dist/src/mocks/index.js +11 -0
- package/dist/src/mocks/index.js.map +1 -0
- package/dist/src/mocks/metrics.d.ts +3 -0
- package/dist/src/mocks/metrics.d.ts.map +1 -0
- package/dist/src/mocks/metrics.js +115 -0
- package/dist/src/mocks/metrics.js.map +1 -0
- package/dist/src/mocks/multiaddr-connection.d.ts +17 -0
- package/dist/src/mocks/multiaddr-connection.d.ts.map +1 -0
- package/dist/src/mocks/multiaddr-connection.js +60 -0
- package/dist/src/mocks/multiaddr-connection.js.map +1 -0
- package/dist/src/mocks/muxer.d.ts +36 -0
- package/dist/src/mocks/muxer.d.ts.map +1 -0
- package/dist/src/mocks/muxer.js +213 -0
- package/dist/src/mocks/muxer.js.map +1 -0
- package/dist/src/mocks/peer-discovery.d.ts +22 -0
- package/dist/src/mocks/peer-discovery.d.ts.map +1 -0
- package/dist/src/mocks/peer-discovery.js +47 -0
- package/dist/src/mocks/peer-discovery.js.map +1 -0
- package/dist/src/mocks/registrar.d.ts +18 -0
- package/dist/src/mocks/registrar.d.ts.map +1 -0
- package/dist/src/mocks/registrar.js +66 -0
- package/dist/src/mocks/registrar.js.map +1 -0
- package/dist/src/mocks/upgrader.d.ts +10 -0
- package/dist/src/mocks/upgrader.d.ts.map +1 -0
- package/dist/src/mocks/upgrader.js +31 -0
- package/dist/src/mocks/upgrader.js.map +1 -0
- package/dist/src/peer-discovery/index.d.ts +5 -0
- package/dist/src/peer-discovery/index.d.ts.map +1 -0
- package/dist/src/peer-discovery/index.js +66 -0
- package/dist/src/peer-discovery/index.js.map +1 -0
- package/dist/src/pubsub/api.d.ts +6 -0
- package/dist/src/pubsub/api.d.ts.map +1 -0
- package/dist/src/pubsub/api.js +87 -0
- package/dist/src/pubsub/api.js.map +1 -0
- package/dist/src/pubsub/connection-handlers.d.ts +6 -0
- package/dist/src/pubsub/connection-handlers.d.ts.map +1 -0
- package/dist/src/pubsub/connection-handlers.js +329 -0
- package/dist/src/pubsub/connection-handlers.js.map +1 -0
- package/dist/src/pubsub/emit-self.d.ts +6 -0
- package/dist/src/pubsub/emit-self.d.ts.map +1 -0
- package/dist/src/pubsub/emit-self.js +80 -0
- package/dist/src/pubsub/emit-self.js.map +1 -0
- package/dist/src/pubsub/index.d.ts +18 -0
- package/dist/src/pubsub/index.d.ts.map +1 -0
- package/dist/src/pubsub/index.js +17 -0
- package/dist/src/pubsub/index.js.map +1 -0
- package/dist/src/pubsub/messages.d.ts +6 -0
- package/dist/src/pubsub/messages.d.ts.map +1 -0
- package/dist/src/pubsub/messages.js +48 -0
- package/dist/src/pubsub/messages.js.map +1 -0
- package/dist/src/pubsub/multiple-nodes.d.ts +6 -0
- package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -0
- package/dist/src/pubsub/multiple-nodes.js +350 -0
- package/dist/src/pubsub/multiple-nodes.js.map +1 -0
- package/dist/src/pubsub/two-nodes.d.ts +6 -0
- package/dist/src/pubsub/two-nodes.d.ts.map +1 -0
- package/dist/src/pubsub/two-nodes.js +217 -0
- package/dist/src/pubsub/two-nodes.js.map +1 -0
- package/dist/src/pubsub/utils.d.ts +6 -0
- package/dist/src/pubsub/utils.d.ts.map +1 -0
- package/dist/src/pubsub/utils.js +22 -0
- package/dist/src/pubsub/utils.js.map +1 -0
- package/dist/src/stream-muxer/base-test.d.ts +5 -0
- package/dist/src/stream-muxer/base-test.d.ts.map +1 -0
- package/dist/src/stream-muxer/base-test.js +153 -0
- package/dist/src/stream-muxer/base-test.js.map +1 -0
- package/dist/src/stream-muxer/close-test.d.ts +5 -0
- package/dist/src/stream-muxer/close-test.d.ts.map +1 -0
- package/dist/src/stream-muxer/close-test.js +357 -0
- package/dist/src/stream-muxer/close-test.js.map +1 -0
- package/dist/src/stream-muxer/fixtures/pb/message.d.ts +13 -0
- package/dist/src/stream-muxer/fixtures/pb/message.d.ts.map +1 -0
- package/dist/src/stream-muxer/fixtures/pb/message.js +67 -0
- package/dist/src/stream-muxer/fixtures/pb/message.js.map +1 -0
- package/dist/src/stream-muxer/index.d.ts +5 -0
- package/dist/src/stream-muxer/index.d.ts.map +1 -0
- package/dist/src/stream-muxer/index.js +13 -0
- package/dist/src/stream-muxer/index.js.map +1 -0
- package/dist/src/stream-muxer/mega-stress-test.d.ts +5 -0
- package/dist/src/stream-muxer/mega-stress-test.d.ts.map +1 -0
- package/dist/src/stream-muxer/mega-stress-test.js +11 -0
- package/dist/src/stream-muxer/mega-stress-test.js.map +1 -0
- package/dist/src/stream-muxer/spawner.d.ts +4 -0
- package/dist/src/stream-muxer/spawner.d.ts.map +1 -0
- package/dist/src/stream-muxer/spawner.js +37 -0
- package/dist/src/stream-muxer/spawner.js.map +1 -0
- package/dist/src/stream-muxer/stress-test.d.ts +5 -0
- package/dist/src/stream-muxer/stress-test.d.ts.map +1 -0
- package/dist/src/stream-muxer/stress-test.js +23 -0
- package/dist/src/stream-muxer/stress-test.js.map +1 -0
- package/dist/src/transport/dial-test.d.ts +5 -0
- package/dist/src/transport/dial-test.d.ts.map +1 -0
- package/dist/src/transport/dial-test.js +98 -0
- package/dist/src/transport/dial-test.js.map +1 -0
- package/dist/src/transport/filter-test.d.ts +5 -0
- package/dist/src/transport/filter-test.d.ts.map +1 -0
- package/dist/src/transport/filter-test.js +18 -0
- package/dist/src/transport/filter-test.js.map +1 -0
- package/dist/src/transport/index.d.ts +15 -0
- package/dist/src/transport/index.d.ts.map +1 -0
- package/dist/src/transport/index.js +11 -0
- package/dist/src/transport/index.js.map +1 -0
- package/dist/src/transport/listen-test.d.ts +5 -0
- package/dist/src/transport/listen-test.d.ts.map +1 -0
- package/dist/src/transport/listen-test.js +152 -0
- package/dist/src/transport/listen-test.js.map +1 -0
- package/package.json +73 -3
- package/src/connection/index.ts +182 -0
- package/src/connection-encryption/index.ts +97 -0
- package/src/connection-encryption/utils/index.ts +24 -0
- package/src/mocks/connection-encrypter.ts +113 -0
- package/src/mocks/connection-gater.ts +18 -0
- package/src/mocks/connection-manager.ts +211 -0
- package/src/mocks/connection.ts +224 -0
- package/src/mocks/duplex.ts +10 -0
- package/src/mocks/index.ts +12 -0
- package/src/mocks/metrics.ts +162 -0
- package/src/mocks/multiaddr-connection.ts +76 -0
- package/src/mocks/muxer.ts +303 -0
- package/src/mocks/peer-discovery.ts +60 -0
- package/src/mocks/registrar.ts +88 -0
- package/src/mocks/upgrader.ts +49 -0
- package/src/peer-discovery/index.ts +90 -0
- package/src/pubsub/api.ts +114 -0
- package/src/pubsub/connection-handlers.ts +413 -0
- package/src/pubsub/emit-self.ts +99 -0
- package/src/pubsub/index.ts +34 -0
- package/src/pubsub/messages.ts +59 -0
- package/src/pubsub/multiple-nodes.ts +440 -0
- package/src/pubsub/two-nodes.ts +273 -0
- package/src/pubsub/utils.ts +29 -0
- package/src/stream-muxer/base-test.ts +196 -0
- package/src/stream-muxer/close-test.ts +442 -0
- package/src/stream-muxer/fixtures/pb/message.proto +7 -0
- package/src/stream-muxer/fixtures/pb/message.ts +87 -0
- package/src/stream-muxer/index.ts +15 -0
- package/src/stream-muxer/mega-stress-test.ts +14 -0
- package/src/stream-muxer/spawner.ts +55 -0
- package/src/stream-muxer/stress-test.ts +27 -0
- package/src/transport/dial-test.ts +124 -0
- package/src/transport/filter-test.ts +25 -0
- package/src/transport/index.ts +25 -0
- package/src/transport/listen-test.ts +191 -0
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
## Table of contents <!-- omit in toc -->
|
|
11
11
|
|
|
12
12
|
- [Install](#install)
|
|
13
|
+
- [Browser `<script>` tag](#browser-script-tag)
|
|
13
14
|
- [Usage](#usage)
|
|
14
15
|
- [API Docs](#api-docs)
|
|
15
16
|
- [License](#license)
|
|
@@ -21,6 +22,14 @@
|
|
|
21
22
|
$ npm i @libp2p/interface-compliance-tests
|
|
22
23
|
```
|
|
23
24
|
|
|
25
|
+
### Browser `<script>` tag
|
|
26
|
+
|
|
27
|
+
Loading this module through a script tag will make it's exports available as `Libp2pInterfaceComplianceTests` in the global namespace.
|
|
28
|
+
|
|
29
|
+
```html
|
|
30
|
+
<script src="https://unpkg.com/@libp2p/interface-compliance-tests/dist/index.min.js"></script>
|
|
31
|
+
```
|
|
32
|
+
|
|
24
33
|
## Usage
|
|
25
34
|
|
|
26
35
|
Each [interface](https://npmjs.org/packages/@libp2p/interfaces) has its documentation on how to use the compliance tests and should be used as the source of truth.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/connection/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAU,MAAM,8BAA8B,CAAA;+BAEhD,UAAU,UAAU,CAAC,KAAG,IAAI;AAAlD,wBA+KC"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { expect } from 'aegir/chai';
|
|
2
|
+
import sinon from 'sinon';
|
|
3
|
+
import { stubInterface } from 'ts-sinon';
|
|
4
|
+
export default (test) => {
|
|
5
|
+
describe('connection', () => {
|
|
6
|
+
describe('open connection', () => {
|
|
7
|
+
let connection;
|
|
8
|
+
beforeEach(async () => {
|
|
9
|
+
connection = await test.setup();
|
|
10
|
+
});
|
|
11
|
+
afterEach(async () => {
|
|
12
|
+
await connection.close();
|
|
13
|
+
await test.teardown();
|
|
14
|
+
});
|
|
15
|
+
it('should have properties set', () => {
|
|
16
|
+
expect(connection.id).to.exist();
|
|
17
|
+
expect(connection.remotePeer).to.exist();
|
|
18
|
+
expect(connection.remoteAddr).to.exist();
|
|
19
|
+
expect(connection.status).to.equal('open');
|
|
20
|
+
expect(connection.timeline.open).to.exist();
|
|
21
|
+
expect(connection.timeline.close).to.not.exist();
|
|
22
|
+
expect(connection.direction).to.exist();
|
|
23
|
+
expect(connection.streams).to.eql([]);
|
|
24
|
+
expect(connection.tags).to.eql([]);
|
|
25
|
+
});
|
|
26
|
+
it('should get the metadata of an open connection', () => {
|
|
27
|
+
expect(connection.status).to.equal('open');
|
|
28
|
+
expect(connection.direction).to.exist();
|
|
29
|
+
expect(connection.timeline.open).to.exist();
|
|
30
|
+
expect(connection.timeline.close).to.not.exist();
|
|
31
|
+
});
|
|
32
|
+
it('should return an empty array of streams', () => {
|
|
33
|
+
const streams = connection.streams;
|
|
34
|
+
expect(streams).to.eql([]);
|
|
35
|
+
});
|
|
36
|
+
it('should be able to create a new stream', async () => {
|
|
37
|
+
expect(connection.streams).to.be.empty();
|
|
38
|
+
const protocolToUse = '/echo/0.0.1';
|
|
39
|
+
const stream = await connection.newStream([protocolToUse]);
|
|
40
|
+
expect(stream).to.have.property('protocol', protocolToUse);
|
|
41
|
+
const connStreams = connection.streams;
|
|
42
|
+
expect(stream).to.exist();
|
|
43
|
+
expect(connStreams).to.exist();
|
|
44
|
+
expect(connStreams).to.have.lengthOf(1);
|
|
45
|
+
expect(connStreams[0]).to.equal(stream);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
describe('close connection', () => {
|
|
49
|
+
let connection;
|
|
50
|
+
let timelineProxy;
|
|
51
|
+
const proxyHandler = {
|
|
52
|
+
set() {
|
|
53
|
+
// @ts-expect-error - TS fails to infer here
|
|
54
|
+
return Reflect.set(...arguments);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
beforeEach(async () => {
|
|
58
|
+
timelineProxy = new Proxy({
|
|
59
|
+
open: Date.now() - 10,
|
|
60
|
+
upgraded: Date.now()
|
|
61
|
+
}, proxyHandler);
|
|
62
|
+
connection = await test.setup();
|
|
63
|
+
connection.timeline = timelineProxy;
|
|
64
|
+
});
|
|
65
|
+
afterEach(async () => {
|
|
66
|
+
await test.teardown();
|
|
67
|
+
});
|
|
68
|
+
it('should be able to close the connection after being created', async () => {
|
|
69
|
+
expect(connection.timeline.close).to.not.exist();
|
|
70
|
+
await connection.close();
|
|
71
|
+
expect(connection.timeline.close).to.exist();
|
|
72
|
+
expect(connection.status).to.equal('closed');
|
|
73
|
+
});
|
|
74
|
+
it('should be able to close the connection after opening a stream', async () => {
|
|
75
|
+
// Open stream
|
|
76
|
+
const protocol = '/echo/0.0.1';
|
|
77
|
+
await connection.newStream([protocol]);
|
|
78
|
+
// Close connection
|
|
79
|
+
expect(connection.timeline.close).to.not.exist();
|
|
80
|
+
await connection.close();
|
|
81
|
+
expect(connection.timeline.close).to.exist();
|
|
82
|
+
expect(connection.status).to.equal('closed');
|
|
83
|
+
});
|
|
84
|
+
it('should properly track streams', async () => {
|
|
85
|
+
// Open stream
|
|
86
|
+
const protocol = '/echo/0.0.1';
|
|
87
|
+
const stream = await connection.newStream([protocol]);
|
|
88
|
+
expect(stream).to.have.property('protocol', protocol);
|
|
89
|
+
// Close stream
|
|
90
|
+
await stream.close();
|
|
91
|
+
expect(connection.streams.filter(s => s.id === stream.id)).to.be.empty();
|
|
92
|
+
});
|
|
93
|
+
it('should track outbound streams', async () => {
|
|
94
|
+
// Open stream
|
|
95
|
+
const protocol = '/echo/0.0.1';
|
|
96
|
+
const stream = await connection.newStream(protocol);
|
|
97
|
+
expect(stream).to.have.property('direction', 'outbound');
|
|
98
|
+
});
|
|
99
|
+
it.skip('should track inbound streams', async () => {
|
|
100
|
+
// Add an remotely opened stream
|
|
101
|
+
const stream = stubInterface();
|
|
102
|
+
connection.addStream(stream);
|
|
103
|
+
expect(stream).to.have.property('direction', 'inbound');
|
|
104
|
+
});
|
|
105
|
+
it('should support a proxy on the timeline', async () => {
|
|
106
|
+
sinon.spy(proxyHandler, 'set');
|
|
107
|
+
expect(connection.timeline.close).to.not.exist();
|
|
108
|
+
await connection.close();
|
|
109
|
+
// @ts-expect-error - fails to infer callCount
|
|
110
|
+
expect(proxyHandler.set.callCount).to.equal(1);
|
|
111
|
+
// @ts-expect-error - fails to infer getCall
|
|
112
|
+
const [obj, key, value] = proxyHandler.set.getCall(0).args;
|
|
113
|
+
expect(obj).to.eql(connection.timeline);
|
|
114
|
+
expect(key).to.equal('close');
|
|
115
|
+
expect(value).to.be.a('number').that.equals(connection.timeline.close);
|
|
116
|
+
});
|
|
117
|
+
it('should fail to create a new stream if the connection is closing', async () => {
|
|
118
|
+
expect(connection.timeline.close).to.not.exist();
|
|
119
|
+
const p = connection.close();
|
|
120
|
+
try {
|
|
121
|
+
const protocol = '/echo/0.0.1';
|
|
122
|
+
await connection.newStream([protocol]);
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
expect(err).to.exist();
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
await p;
|
|
130
|
+
}
|
|
131
|
+
throw new Error('should fail to create a new stream if the connection is closing');
|
|
132
|
+
});
|
|
133
|
+
it('should fail to create a new stream if the connection is closed', async () => {
|
|
134
|
+
expect(connection.timeline.close).to.not.exist();
|
|
135
|
+
await connection.close();
|
|
136
|
+
try {
|
|
137
|
+
const protocol = '/echo/0.0.1';
|
|
138
|
+
await connection.newStream([protocol]);
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
expect(err).to.exist();
|
|
142
|
+
expect(err.code).to.equal('ERR_CONNECTION_CLOSED');
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
throw new Error('should fail to create a new stream if the connection is closing');
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/connection/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAIxC,eAAe,CAAC,IAA2B,EAAQ,EAAE;IACnD,QAAQ,CAAC,YAAY,EAAE,GAAG,EAAE;QAC1B,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;YAC/B,IAAI,UAAsB,CAAA;YAE1B,UAAU,CAAC,KAAK,I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ACJ,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/connection-encryption/index.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wCAAwC,CAAA;iCAGzD,UAAU,mBAAmB,CAAC,KAAG,IAAI;AAA7D,wBAoFC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { UnexpectedPeerError } from '@libp2p/interface/errors';
|
|
2
|
+
import * as PeerIdFactory from '@libp2p/peer-id-factory';
|
|
3
|
+
import { expect } from 'aegir/chai';
|
|
4
|
+
import all from 'it-all';
|
|
5
|
+
import { pipe } from 'it-pipe';
|
|
6
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
7
|
+
import peers from '../peers.js';
|
|
8
|
+
import { createMaConnPair } from './utils/index.js';
|
|
9
|
+
export default (common) => {
|
|
10
|
+
describe('interface-connection-encrypter compliance tests', () => {
|
|
11
|
+
let crypto;
|
|
12
|
+
let localPeer;
|
|
13
|
+
let remotePeer;
|
|
14
|
+
let mitmPeer;
|
|
15
|
+
before(async () => {
|
|
16
|
+
[
|
|
17
|
+
crypto,
|
|
18
|
+
localPeer,
|
|
19
|
+
remotePeer,
|
|
20
|
+
mitmPeer
|
|
21
|
+
] = await Promise.all([
|
|
22
|
+
common.setup(),
|
|
23
|
+
PeerIdFactory.createFromJSON(peers[0]),
|
|
24
|
+
PeerIdFactory.createFromJSON(peers[1]),
|
|
25
|
+
PeerIdFactory.createFromJSON(peers[2])
|
|
26
|
+
]);
|
|
27
|
+
});
|
|
28
|
+
after(async () => {
|
|
29
|
+
await common.teardown();
|
|
30
|
+
});
|
|
31
|
+
it('has a protocol string', () => {
|
|
32
|
+
expect(crypto.protocol).to.exist();
|
|
33
|
+
expect(crypto.protocol).to.be.a('string');
|
|
34
|
+
});
|
|
35
|
+
it('it wraps the provided duplex connection', async () => {
|
|
36
|
+
const [localConn, remoteConn] = createMaConnPair();
|
|
37
|
+
const [inboundResult, outboundResult] = await Promise.all([
|
|
38
|
+
crypto.secureInbound(remotePeer, localConn),
|
|
39
|
+
crypto.secureOutbound(localPeer, remoteConn, remotePeer)
|
|
40
|
+
]);
|
|
41
|
+
// Echo server
|
|
42
|
+
void pipe(inboundResult.conn, inboundResult.conn);
|
|
43
|
+
// Send some data and collect the result
|
|
44
|
+
const input = uint8ArrayFromString('data to encrypt');
|
|
45
|
+
const result = await pipe([input], outboundResult.conn, async (source) => all(source));
|
|
46
|
+
expect(result).to.eql([input]);
|
|
47
|
+
});
|
|
48
|
+
it('should return the remote peer id', async () => {
|
|
49
|
+
const [localConn, remoteConn] = createMaConnPair();
|
|
50
|
+
const [inboundResult, outboundResult] = await Promise.all([
|
|
51
|
+
crypto.secureInbound(remotePeer, localConn),
|
|
52
|
+
crypto.secureOutbound(localPeer, remoteConn, remotePeer)
|
|
53
|
+
]);
|
|
54
|
+
// Inbound should return the initiator (local) peer
|
|
55
|
+
expect(inboundResult.remotePeer.toBytes()).to.equalBytes(localPeer.toBytes());
|
|
56
|
+
// Outbound should return the receiver (remote) peer
|
|
57
|
+
expect(outboundResult.remotePeer.toBytes()).to.equalBytes(remotePeer.toBytes());
|
|
58
|
+
});
|
|
59
|
+
it('inbound connections should verify peer integrity if known', async () => {
|
|
60
|
+
const [localConn, remoteConn] = createMaConnPair();
|
|
61
|
+
await Promise.all([
|
|
62
|
+
crypto.secureInbound(remotePeer, localConn, mitmPeer),
|
|
63
|
+
crypto.secureOutbound(localPeer, remoteConn, remotePeer)
|
|
64
|
+
]).then(() => expect.fail(), (err) => {
|
|
65
|
+
expect(err).to.exist();
|
|
66
|
+
expect(err).to.have.property('code', UnexpectedPeerError.code);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/connection-encryption/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AAC9D,OAAO,KAAK,aAAa,MAAM,yBAAyB,CAAA;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACnC,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,KAAK,MAAM,aAAa,CAAA;AAC/B,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AAKnD,eAAe,CAAC,MAAsC,EAAQ,EAAE;IAC9D,QAAQ,CAAC,iDAAiD,EAAE,GAAG,EAAE;QAC/D,IAAI,MAA2B,CAAA;QAC/B,IAAI,SAAiB,CAAA;QACrB,IAAI,UAAkB,CAAA;QACtB,IAAI,QAAgB,CAAA;QAEpB,MAAM,CAAC,KAAK,IAAI,EAAE;YAChB;gBACE,MAAM;gBACN,SAAS;gBACT,UAAU;gBACV,QAAQ;aACT,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;gBACpB,MAAM,CAAC,KAAK,EAAE;gBACd,aAAa,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBACtC,aAAa,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBACtC,aAAa,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;aACvC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;QAEF,KAAK,CAAC,KAAK,IAAI,EAAE;YACf,MAAM,MAAM,CAAC,QAAQ,EAAE,CAAA;QACzB,CAAC,CAAC,CAAA;QAEF,EAA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AK,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAA;AAGvE,wBAAgB,gBAAgB,IAAK,CAAC,mBAAmB,EAAE,mBAAmB,CAAC,CAkB9E"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { multiaddr } from '@multiformats/multiaddr';
|
|
2
|
+
import { duplexPair } from 'it-pair/duplex';
|
|
3
|
+
export function createMaConnPair() {
|
|
4
|
+
const [local, remote] = duplexPair();
|
|
5
|
+
function duplexToMaConn(duplex) {
|
|
6
|
+
const output = {
|
|
7
|
+
...duplex,
|
|
8
|
+
close: async () => { },
|
|
9
|
+
abort: () => { },
|
|
10
|
+
remoteAddr: multiaddr('/ip4/127.0.0.1/tcp/4001'),
|
|
11
|
+
timeline: {
|
|
12
|
+
open: Date.now()
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
return output;
|
|
16
|
+
}
|
|
17
|
+
return [duplexToMaConn(local), duplexToMaConn(remote)];
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/connection-encryption/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAI3C,MAAM,UAAU,gBAAgB;IAC9B,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,UAAU,EAAc,CAAA;IAEhD,SAAS,cAAc,CAAE,MAA6E;QACpG,MAAM,MAAM,GAAwB;YAClC,GAAG,MAAM;YACT,KAAK,EAAE,KAAK,IAAI,EAAE,GAAE,CAAC;YACrB,KAAK,EAAE,GAAG,EAAE,GAAE,CAAC;YACf,UAAU,EAAE,SAAS,CAAC,yBAAyB,CAAC;YAChD,QAAQ,EAAE;gBACR,IAAI,EAAE,IAAI,CAAC,GAAG,EAAE;aACjB;SACF,CAAA;QAED,OAAO,MAAM,CAAA;IACf,CAAC;IAED,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC,CAAA;AACxD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection-encrypter.d.ts","sourceRoot":"","sources":["../../../src/mocks/connection-encrypter.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,wCAAwC,CAAA;AAYjF,wBAAgB,uBAAuB,IAAK,mBAAmB,CA6F9D"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { UnexpectedPeerError } from '@libp2p/interface/errors';
|
|
2
|
+
import { peerIdFromBytes } from '@libp2p/peer-id';
|
|
3
|
+
import { multiaddr } from '@multiformats/multiaddr';
|
|
4
|
+
import { handshake } from 'it-handshake';
|
|
5
|
+
import map from 'it-map';
|
|
6
|
+
import { duplexPair } from 'it-pair/duplex';
|
|
7
|
+
import { pipe } from 'it-pipe';
|
|
8
|
+
// A basic transform that does nothing to the data
|
|
9
|
+
const transform = () => {
|
|
10
|
+
return (source) => (async function* () {
|
|
11
|
+
for await (const chunk of source) {
|
|
12
|
+
yield chunk;
|
|
13
|
+
}
|
|
14
|
+
})();
|
|
15
|
+
};
|
|
16
|
+
export function mockConnectionEncrypter() {
|
|
17
|
+
const encrypter = {
|
|
18
|
+
protocol: 'insecure',
|
|
19
|
+
secureInbound: async (localPeer, duplex, expectedPeer) => {
|
|
20
|
+
// 1. Perform a basic handshake.
|
|
21
|
+
const shake = handshake(duplex);
|
|
22
|
+
shake.write(localPeer.toBytes());
|
|
23
|
+
const remoteId = await shake.read();
|
|
24
|
+
if (remoteId == null) {
|
|
25
|
+
throw new Error('Could not read remote ID');
|
|
26
|
+
}
|
|
27
|
+
const remotePeer = peerIdFromBytes(remoteId.slice());
|
|
28
|
+
shake.rest();
|
|
29
|
+
if (expectedPeer?.equals(remotePeer) === false) {
|
|
30
|
+
throw new UnexpectedPeerError();
|
|
31
|
+
}
|
|
32
|
+
// 2. Create your encryption box/unbox wrapper
|
|
33
|
+
const wrapper = duplexPair();
|
|
34
|
+
const encrypt = transform(); // Use transform iterables to modify data
|
|
35
|
+
const decrypt = transform();
|
|
36
|
+
void pipe(wrapper[0], // We write to wrapper
|
|
37
|
+
encrypt, // The data is encrypted
|
|
38
|
+
shake.stream, // It goes to the remote peer
|
|
39
|
+
// It goes to the remote peer
|
|
40
|
+
source => map(source, (list) => list.subarray()), // turn lists into arrays
|
|
41
|
+
decrypt, // Decrypt the incoming data
|
|
42
|
+
wrapper[0] // Pipe to the wrapper
|
|
43
|
+
);
|
|
44
|
+
return {
|
|
45
|
+
conn: {
|
|
46
|
+
...wrapper[1],
|
|
47
|
+
close: async () => { },
|
|
48
|
+
localAddr: multiaddr('/ip4/127.0.0.1/tcp/4001'),
|
|
49
|
+
remoteAddr: multiaddr('/ip4/127.0.0.1/tcp/4002'),
|
|
50
|
+
timeline: {
|
|
51
|
+
open: Date.now()
|
|
52
|
+
},
|
|
53
|
+
conn: true
|
|
54
|
+
},
|
|
55
|
+
remotePeer,
|
|
56
|
+
remoteExtensions: {}
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
secureOutbound: async (localPeer, duplex, remotePeer) => {
|
|
60
|
+
// 1. Perform a basic handshake.
|
|
61
|
+
const shake = handshake(duplex);
|
|
62
|
+
shake.write(localPeer.toBytes());
|
|
63
|
+
const remoteId = await shake.read();
|
|
64
|
+
if (remoteId == null) {
|
|
65
|
+
throw new Error('Could not read remote ID');
|
|
66
|
+
}
|
|
67
|
+
shake.rest();
|
|
68
|
+
// 2. Create your encryption box/unbox wrapper
|
|
69
|
+
const wrapper = duplexPair();
|
|
70
|
+
const encrypt = transform();
|
|
71
|
+
const decrypt = transform();
|
|
72
|
+
void pipe(wrapper[0], // We write to wrapper
|
|
73
|
+
encrypt, // The data is encrypted
|
|
74
|
+
shake.stream, // It goes to the remote peer
|
|
75
|
+
// It goes to the remote peer
|
|
76
|
+
source => map(source, (list) => list.subarray()), // turn lists into arrays
|
|
77
|
+
decrypt, // Decrypt the incoming data
|
|
78
|
+
wrapper[0] // Pipe to the wrapper
|
|
79
|
+
);
|
|
80
|
+
return {
|
|
81
|
+
conn: {
|
|
82
|
+
...wrapper[1],
|
|
83
|
+
close: async () => { },
|
|
84
|
+
localAddr: multiaddr('/ip4/127.0.0.1/tcp/4001'),
|
|
85
|
+
remoteAddr: multiaddr('/ip4/127.0.0.1/tcp/4002'),
|
|
86
|
+
timeline: {
|
|
87
|
+
open: Date.now()
|
|
88
|
+
},
|
|
89
|
+
conn: true
|
|
90
|
+
},
|
|
91
|
+
remotePeer: peerIdFromBytes(remoteId.slice()),
|
|
92
|
+
remoteExtensions: {}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
return encrypter;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=connection-encrypter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection-encrypter.js","sourceRoot":"","sources":["../../../src/mocks/connection-encrypter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AAC9D,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAI9B,kDAAkD;AAClD,MAAM,SAAS,GAAG,GAA+C,EAAE;IACjE,OAAO,CAAC,MAAiB,EAAE,EAAE,CAAC,CAAC,KAAK,SAAU,CAAC;QAC7C,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM,EAAE;YAChC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,4BAA4B;YACrC,OAAO,CAAC,CAAC,CAAC,CAAC,sBAAsB;aAClC,CAAA;YAED,OAAO;gBACL,IAAI,EAAE;oBACJ,GAAG,OAAO,CAAC,CAAC,CAAC;oBACb,KAAK,EAAE,KAAK,IAAI,EAAE,GAAG,CAAC;oBACtB,SAAS,EAAE,SAAS,CAAC,yBAAyB,CAAC;oBAC/C,UAAU,EAAE,SAAS,CAAC,yBAAyB,CAAC;oBAChD,QAAQ,EAAE;wBACR,IAAI,EAAE,IAAI,CAAC,GAAG,EAAE;qBACjB;oBACD,IAAI,EAAE,IAAI;iBACX;gBACD,UAAU,EAAE,eAAe,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;gBAC7C,gBAAgB,EAAE,EAAE;aACrB,CAAA;QACH,CAAC;KACF,CAAA;IAED,OAAO,SAAS,CAAA;AAClB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection-gater.d.ts","sourceRoot":"","sources":["../../../src/mocks/connection-gater.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAA;AAEzE,wBAAgB,mBAAmB,IAAK,eAAe,CAetD"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function mockConnectionGater() {
|
|
2
|
+
return {
|
|
3
|
+
denyDialPeer: async () => Promise.resolve(false),
|
|
4
|
+
denyDialMultiaddr: async () => Promise.resolve(false),
|
|
5
|
+
denyInboundConnection: async () => Promise.resolve(false),
|
|
6
|
+
denyOutboundConnection: async () => Promise.resolve(false),
|
|
7
|
+
denyInboundEncryptedConnection: async () => Promise.resolve(false),
|
|
8
|
+
denyOutboundEncryptedConnection: async () => Promise.resolve(false),
|
|
9
|
+
denyInboundUpgradedConnection: async () => Promise.resolve(false),
|
|
10
|
+
denyOutboundUpgradedConnection: async () => Promise.resolve(false),
|
|
11
|
+
denyInboundRelayReservation: async () => Promise.resolve(false),
|
|
12
|
+
denyOutboundRelayedConnection: async () => Promise.resolve(false),
|
|
13
|
+
denyInboundRelayedConnection: async () => Promise.resolve(false),
|
|
14
|
+
filterMultiaddrForPeer: async () => Promise.resolve(true)
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=connection-gater.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection-gater.js","sourceRoot":"","sources":["../../../src/mocks/connection-gater.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,mBAAmB;IACjC,OAAO;QACL,YAAY,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC;QAChD,iBAAiB,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC;QACrD,qBAAqB,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC;QACzD,sBAAsB,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC;QAC1D,8BAA8B,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC;QAClE,+BAA+B,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC;QACnE,6BAA6B,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC;QACjE,8BAA8B,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC;QAClE,2BAA2B,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC;QAC/D,6BAA6B,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC;QACjE,4BAA4B,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC;QAChE,sBAAsB,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC;KAC1D,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type PeerId } from '@libp2p/interface/peer-id';
|
|
2
|
+
import { type Multiaddr } from '@multiformats/multiaddr';
|
|
3
|
+
import type { Libp2pEvents } from '@libp2p/interface';
|
|
4
|
+
import type { EventEmitter } from '@libp2p/interface/events';
|
|
5
|
+
import type { PubSub } from '@libp2p/interface/pubsub';
|
|
6
|
+
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager';
|
|
7
|
+
import type { Registrar } from '@libp2p/interface-internal/registrar';
|
|
8
|
+
export interface MockNetworkComponents {
|
|
9
|
+
peerId: PeerId;
|
|
10
|
+
registrar: Registrar;
|
|
11
|
+
connectionManager: ConnectionManager;
|
|
12
|
+
events: EventEmitter<Libp2pEvents>;
|
|
13
|
+
pubsub?: PubSub;
|
|
14
|
+
}
|
|
15
|
+
declare class MockNetwork {
|
|
16
|
+
private components;
|
|
17
|
+
addNode(components: MockNetworkComponents): void;
|
|
18
|
+
getNode(peerId: PeerId | Multiaddr[]): MockNetworkComponents;
|
|
19
|
+
reset(): void;
|
|
20
|
+
}
|
|
21
|
+
export declare const mockNetwork: MockNetwork;
|
|
22
|
+
export interface MockConnectionManagerComponents {
|
|
23
|
+
peerId: PeerId;
|
|
24
|
+
registrar: Registrar;
|
|
25
|
+
events: EventEmitter<Libp2pEvents>;
|
|
26
|
+
}
|
|
27
|
+
export declare function mockConnectionManager(components: MockConnectionManagerComponents): ConnectionManager;
|
|
28
|
+
export {};
|
|
29
|
+
//# sourceMappingURL=connection-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection-manager.d.ts","sourceRoot":"","sources":["../../../src/mocks/connection-manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAY,KAAK,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAGjE,OAAO,EAAe,KAAK,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAErE,OAAO,KAAK,EAAE,YAAY,EAAe,MAAM,mBAAmB,CAAA;AAElE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AAEtD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,+CAA+C,CAAA;AACtF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAA;AAErE,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,EAAE,SAAS,CAAA;IACpB,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,MAAM,EAAE,YAAY,CAAC,YAAY,CAAC,CAAA;IAClC,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,cAAM,WAAW;IACf,OAAO,CAAC,UAAU,CAA8B;IAEhD,OAAO,CAAE,UAAU,EAAE,qBAAqB,GAAG,IAAI;IAIjD,OAAO,CAAE,MAAM,EAAE,MAAM,GAAG,SAAS,EAAG,GAAG,qBAAqB;IAc9D,KAAK,IAAK,IAAI;CAGf;AAED,eAAO,MAAM,WAAW,aAAoB,CAAA;AAE5C,MAAM,WAAW,+BAA+B;IAC9C,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,EAAE,SAAS,CAAA;IACpB,MAAM,EAAE,YAAY,CAAC,YAAY,CAAC,CAAA;CACnC;AA0JD,wBAAgB,qBAAqB,CAAE,UAAU,EAAE,+BAA+B,GAAG,iBAAiB,CAErG"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors';
|
|
2
|
+
import { isPeerId } from '@libp2p/interface/peer-id';
|
|
3
|
+
import { PeerMap } from '@libp2p/peer-collections';
|
|
4
|
+
import { peerIdFromString } from '@libp2p/peer-id';
|
|
5
|
+
import { isMultiaddr } from '@multiformats/multiaddr';
|
|
6
|
+
import { connectionPair } from './connection.js';
|
|
7
|
+
class MockNetwork {
|
|
8
|
+
components = [];
|
|
9
|
+
addNode(components) {
|
|
10
|
+
this.components.push(components);
|
|
11
|
+
}
|
|
12
|
+
getNode(peerId) {
|
|
13
|
+
if (Array.isArray(peerId) && peerId.length > 0) {
|
|
14
|
+
peerId = peerIdFromString(peerId[0].getPeerId() ?? '');
|
|
15
|
+
}
|
|
16
|
+
else if (isPeerId(peerId)) {
|
|
17
|
+
for (const components of this.components) {
|
|
18
|
+
if (peerId.equals(components.peerId)) {
|
|
19
|
+
return components;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
throw new CodeError('Peer not found', 'ERR_PEER_NOT_FOUND');
|
|
24
|
+
}
|
|
25
|
+
reset() {
|
|
26
|
+
this.components = [];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export const mockNetwork = new MockNetwork();
|
|
30
|
+
class MockConnectionManager {
|
|
31
|
+
connections = [];
|
|
32
|
+
components;
|
|
33
|
+
started = false;
|
|
34
|
+
constructor(components) {
|
|
35
|
+
this.components = components;
|
|
36
|
+
}
|
|
37
|
+
isStarted() {
|
|
38
|
+
return this.started;
|
|
39
|
+
}
|
|
40
|
+
async start() {
|
|
41
|
+
this.started = true;
|
|
42
|
+
}
|
|
43
|
+
async stop() {
|
|
44
|
+
for (const connection of this.connections) {
|
|
45
|
+
await this.closeConnections(connection.remotePeer);
|
|
46
|
+
}
|
|
47
|
+
this.started = false;
|
|
48
|
+
}
|
|
49
|
+
getConnections(peerId) {
|
|
50
|
+
if (peerId != null) {
|
|
51
|
+
return this.connections
|
|
52
|
+
.filter(c => c.remotePeer.toString() === peerId.toString());
|
|
53
|
+
}
|
|
54
|
+
return this.connections;
|
|
55
|
+
}
|
|
56
|
+
getConnectionsMap() {
|
|
57
|
+
const map = new PeerMap();
|
|
58
|
+
for (const conn of this.connections) {
|
|
59
|
+
const conns = map.get(conn.remotePeer) ?? [];
|
|
60
|
+
conns.push(conn);
|
|
61
|
+
map.set(conn.remotePeer, conns);
|
|
62
|
+
}
|
|
63
|
+
return map;
|
|
64
|
+
}
|
|
65
|
+
async openConnection(peerId) {
|
|
66
|
+
if (this.components == null) {
|
|
67
|
+
throw new CodeError('Not initialized', 'ERR_NOT_INITIALIZED');
|
|
68
|
+
}
|
|
69
|
+
if (isMultiaddr(peerId)) {
|
|
70
|
+
throw new CodeError('Dialing multiaddrs not supported', 'ERR_NOT_SUPPORTED');
|
|
71
|
+
}
|
|
72
|
+
let existingConnections = [];
|
|
73
|
+
if (Array.isArray(peerId) && peerId.length > 0) {
|
|
74
|
+
existingConnections = this.getConnections(peerIdFromString(peerId[0].getPeerId() ?? ''));
|
|
75
|
+
}
|
|
76
|
+
else if (isPeerId(peerId)) {
|
|
77
|
+
existingConnections = this.getConnections(peerId);
|
|
78
|
+
}
|
|
79
|
+
if (existingConnections.length > 0) {
|
|
80
|
+
return existingConnections[0];
|
|
81
|
+
}
|
|
82
|
+
const componentsB = mockNetwork.getNode(peerId);
|
|
83
|
+
const [aToB, bToA] = connectionPair(this.components, componentsB);
|
|
84
|
+
// track connections
|
|
85
|
+
this.connections.push(aToB);
|
|
86
|
+
componentsB.connectionManager.connections.push(bToA);
|
|
87
|
+
this.components.events.safeDispatchEvent('connection:open', {
|
|
88
|
+
detail: aToB
|
|
89
|
+
});
|
|
90
|
+
for (const protocol of this.components.registrar.getProtocols()) {
|
|
91
|
+
for (const topology of this.components.registrar.getTopologies(protocol)) {
|
|
92
|
+
topology.onConnect?.(componentsB.peerId, aToB);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
this.components.events.safeDispatchEvent('peer:connect', { detail: componentsB.peerId });
|
|
96
|
+
componentsB.events.safeDispatchEvent('connection:open', {
|
|
97
|
+
detail: bToA
|
|
98
|
+
});
|
|
99
|
+
for (const protocol of componentsB.registrar.getProtocols()) {
|
|
100
|
+
for (const topology of componentsB.registrar.getTopologies(protocol)) {
|
|
101
|
+
topology.onConnect?.(this.components.peerId, bToA);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
componentsB.events.safeDispatchEvent('peer:connect', { detail: this.components.peerId });
|
|
105
|
+
return aToB;
|
|
106
|
+
}
|
|
107
|
+
async closeConnections(peerId) {
|
|
108
|
+
if (this.components == null) {
|
|
109
|
+
throw new CodeError('Not initialized', 'ERR_NOT_INITIALIZED');
|
|
110
|
+
}
|
|
111
|
+
const connections = this.getConnections(peerId);
|
|
112
|
+
if (connections.length === 0) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const componentsB = mockNetwork.getNode(peerId);
|
|
116
|
+
for (const protocol of this.components.registrar.getProtocols()) {
|
|
117
|
+
this.components.registrar.getTopologies(protocol).forEach(topology => {
|
|
118
|
+
topology.onDisconnect?.(componentsB.peerId);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
for (const conn of connections) {
|
|
122
|
+
await conn.close();
|
|
123
|
+
}
|
|
124
|
+
this.connections = this.connections.filter(c => !c.remotePeer.equals(peerId));
|
|
125
|
+
if (this.connections.filter(c => !c.remotePeer.equals(peerId)).length === 0) {
|
|
126
|
+
componentsB.events.safeDispatchEvent('peer:disconnect', { detail: peerId });
|
|
127
|
+
}
|
|
128
|
+
await componentsB.connectionManager?.closeConnections(this.components.peerId);
|
|
129
|
+
if (componentsB.connectionManager?.getConnectionsMap().get(this.components.peerId) == null) {
|
|
130
|
+
componentsB.events.safeDispatchEvent('peer:disconnect', { detail: this.components.peerId });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
async acceptIncomingConnection() {
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
afterUpgradeInbound() {
|
|
137
|
+
}
|
|
138
|
+
getDialQueue() {
|
|
139
|
+
return [];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
export function mockConnectionManager(components) {
|
|
143
|
+
return new MockConnectionManager(components);
|
|
144
|
+
}
|
|
145
|
+
//# sourceMappingURL=connection-manager.js.map
|