@libp2p/interface-compliance-tests 3.0.7-d853d124 → 3.0.7-e9cafd3d
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 +18 -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 +158 -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 +51 -0
- package/dist/src/mocks/multiaddr-connection.js.map +1 -0
- package/dist/src/mocks/muxer.d.ts +8 -0
- package/dist/src/mocks/muxer.d.ts.map +1 -0
- package/dist/src/mocks/muxer.js +339 -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 +269 -0
- package/dist/src/stream-muxer/close-test.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 +36 -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 +69 -3
- package/src/connection/index.ts +182 -0
- package/src/connection-encryption/index.ts +97 -0
- package/src/connection-encryption/utils/index.ts +23 -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 +215 -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 +67 -0
- package/src/mocks/muxer.ts +445 -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 +346 -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 +54 -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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/transport/index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,gBAAgB,CAAA;AACjC,OAAO,MAAM,MAAM,kBAAkB,CAAA;AACrC,OAAO,MAAM,MAAM,kBAAkB,CAAA;AAgBrC,eAAe,CAAC,MAAwC,EAAQ,EAAE;IAChE,QAAQ,CAAC,qBAAqB,EAAE,GAAG,EAAE;QACnC,IAAI,CAAC,MAAM,CAAC,CAAA;QACZ,MAAM,CAAC,MAAM,CAAC,CAAA;QACd,MAAM,CAAC,MAAM,CAAC,CAAA;IAChB,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"listen-test.d.ts","sourceRoot":"","sources":["../../../src/transport/listen-test.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAA;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;iCAMpB,UAAU,qBAAqB,CAAC,KAAG,IAAI;AAA/D,wBA4KC"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/* eslint max-nested-callbacks: ["error", 8] */
|
|
2
|
+
import { CustomEvent, EventEmitter } from '@libp2p/interface/events';
|
|
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
|
+
export default (common) => {
|
|
13
|
+
describe('listen', () => {
|
|
14
|
+
let upgrader;
|
|
15
|
+
let addrs;
|
|
16
|
+
let transport;
|
|
17
|
+
let registrar;
|
|
18
|
+
before(async () => {
|
|
19
|
+
registrar = mockRegistrar();
|
|
20
|
+
upgrader = mockUpgrader({
|
|
21
|
+
registrar,
|
|
22
|
+
events: new EventEmitter()
|
|
23
|
+
});
|
|
24
|
+
({ transport, addrs } = await common.setup());
|
|
25
|
+
});
|
|
26
|
+
after(async () => {
|
|
27
|
+
await common.teardown();
|
|
28
|
+
});
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
sinon.restore();
|
|
31
|
+
});
|
|
32
|
+
it('simple', async () => {
|
|
33
|
+
const listener = transport.createListener({
|
|
34
|
+
upgrader
|
|
35
|
+
});
|
|
36
|
+
await listener.listen(addrs[0]);
|
|
37
|
+
await listener.close();
|
|
38
|
+
});
|
|
39
|
+
it('close listener with connections, through timeout', async () => {
|
|
40
|
+
const upgradeSpy = sinon.spy(upgrader, 'upgradeInbound');
|
|
41
|
+
const listenerConns = [];
|
|
42
|
+
const protocol = '/test/protocol';
|
|
43
|
+
void registrar.handle(protocol, (data) => {
|
|
44
|
+
void drain(data.stream.source);
|
|
45
|
+
});
|
|
46
|
+
const listener = transport.createListener({
|
|
47
|
+
upgrader,
|
|
48
|
+
handler: (conn) => {
|
|
49
|
+
listenerConns.push(conn);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
// Listen
|
|
53
|
+
await listener.listen(addrs[0]);
|
|
54
|
+
// Create two connections to the listener
|
|
55
|
+
const [conn1] = await Promise.all([
|
|
56
|
+
transport.dial(addrs[0], {
|
|
57
|
+
upgrader
|
|
58
|
+
}),
|
|
59
|
+
transport.dial(addrs[0], {
|
|
60
|
+
upgrader
|
|
61
|
+
})
|
|
62
|
+
]);
|
|
63
|
+
// Give the listener a chance to finish its upgrade
|
|
64
|
+
await pWaitFor(() => listenerConns.length === 2);
|
|
65
|
+
const stream1 = await conn1.newStream([protocol]);
|
|
66
|
+
// Wait for the data send and close to finish
|
|
67
|
+
await Promise.all([
|
|
68
|
+
pipe([uint8ArrayFromString('Some data that is never handled')], stream1),
|
|
69
|
+
// Closer the listener (will take a couple of seconds to time out)
|
|
70
|
+
listener.close()
|
|
71
|
+
]);
|
|
72
|
+
stream1.close();
|
|
73
|
+
await conn1.close();
|
|
74
|
+
expect(isValidTick(conn1.timeline.close)).to.equal(true);
|
|
75
|
+
listenerConns.forEach(conn => {
|
|
76
|
+
expect(isValidTick(conn.timeline.close)).to.equal(true);
|
|
77
|
+
});
|
|
78
|
+
// 2 dials = 2 connections upgraded
|
|
79
|
+
expect(upgradeSpy.callCount).to.equal(2);
|
|
80
|
+
});
|
|
81
|
+
it('should not handle connection if upgradeInbound throws', async () => {
|
|
82
|
+
sinon.stub(upgrader, 'upgradeInbound').throws();
|
|
83
|
+
const listener = transport.createListener({
|
|
84
|
+
upgrader
|
|
85
|
+
});
|
|
86
|
+
// Listen
|
|
87
|
+
await listener.listen(addrs[0]);
|
|
88
|
+
// Create a connection to the listener
|
|
89
|
+
const conn = await transport.dial(addrs[0], {
|
|
90
|
+
upgrader
|
|
91
|
+
});
|
|
92
|
+
await pWaitFor(() => typeof conn.timeline.close === 'number');
|
|
93
|
+
await listener.close();
|
|
94
|
+
});
|
|
95
|
+
describe('events', () => {
|
|
96
|
+
it('connection', async () => {
|
|
97
|
+
const upgradeSpy = sinon.spy(upgrader, 'upgradeInbound');
|
|
98
|
+
const listener = transport.createListener({
|
|
99
|
+
upgrader
|
|
100
|
+
});
|
|
101
|
+
const deferred = defer();
|
|
102
|
+
let conn;
|
|
103
|
+
listener.addEventListener('connection', (evt) => {
|
|
104
|
+
conn = evt.detail;
|
|
105
|
+
deferred.resolve();
|
|
106
|
+
});
|
|
107
|
+
void (async () => {
|
|
108
|
+
await listener.listen(addrs[0]);
|
|
109
|
+
await transport.dial(addrs[0], {
|
|
110
|
+
upgrader
|
|
111
|
+
});
|
|
112
|
+
})();
|
|
113
|
+
await deferred.promise;
|
|
114
|
+
await expect(upgradeSpy.getCall(0).returnValue).to.eventually.equal(conn);
|
|
115
|
+
expect(upgradeSpy.callCount).to.equal(1);
|
|
116
|
+
await listener.close();
|
|
117
|
+
});
|
|
118
|
+
it('listening', (done) => {
|
|
119
|
+
const listener = transport.createListener({
|
|
120
|
+
upgrader
|
|
121
|
+
});
|
|
122
|
+
listener.addEventListener('listening', () => {
|
|
123
|
+
listener.close().then(done, done);
|
|
124
|
+
});
|
|
125
|
+
void listener.listen(addrs[0]);
|
|
126
|
+
});
|
|
127
|
+
it('error', (done) => {
|
|
128
|
+
const listener = transport.createListener({
|
|
129
|
+
upgrader
|
|
130
|
+
});
|
|
131
|
+
listener.addEventListener('error', (evt) => {
|
|
132
|
+
expect(evt.detail).to.be.an.instanceOf(Error);
|
|
133
|
+
listener.close().then(done, done);
|
|
134
|
+
});
|
|
135
|
+
listener.dispatchEvent(new CustomEvent('error', {
|
|
136
|
+
detail: new Error('my err')
|
|
137
|
+
}));
|
|
138
|
+
});
|
|
139
|
+
it('close', (done) => {
|
|
140
|
+
const listener = transport.createListener({
|
|
141
|
+
upgrader
|
|
142
|
+
});
|
|
143
|
+
listener.addEventListener('close', () => { done(); });
|
|
144
|
+
void (async () => {
|
|
145
|
+
await listener.listen(addrs[0]);
|
|
146
|
+
await listener.close();
|
|
147
|
+
})();
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
//# sourceMappingURL=listen-test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"listen-test.js","sourceRoot":"","sources":["../../../src/transport/listen-test.ts"],"names":[],"mappings":"AAAA,+CAA+C;AAC/C,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AACpE,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,MAAM,UAAU,CAAA;AAC5B,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,KAAK,MAAM,SAAS,CAAA;AAC3B,OAAO,QAAQ,MAAM,YAAY,CAAA;AACjC,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACjD,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAQ/D,eAAe,CAAC,MAAwC,EAAQ,EAAE;IAChE,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;QACtB,IAAI,QAAkB,CAAA;QACtB,IAAI,KAAkB,CAAA;QACtB,IAAI,SAAoB,CAAA;QACxB,IAAI,SAAoB,CAAA;QAExB,MAAM,CAAC,KAAK,IAAI,EAAE;YAChB,SAAS,GAAG,aAAa,EAAE,CAAA;YAC3B,QAAQ,GAAG,YAAY,CAAC;gBACtB,SAAS;gBACT,MAAM,EAAE,IAAI,YAAY,EAAE;aAC3B,CAAC,CAAC;YAEH,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,MAAM,MAA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AE;oBAC9C,MAAM,EAAE,IAAI,KAAK,CAAC,QAAQ,CAAC;iBAC5B,CAAC,CAAC,CAAA;YACL,CAAC,CAAC,CAAA;YAEF,EAAE,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE;gBACnB,MAAM,QAAQ,GAAG,SAAS,CAAC,cAAc,CAAC;oBACxC,QAAQ;iBACT,CAAC,CAAA;gBACF,QAAQ,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,CAAA,CAAC,CAAC,CAAC,CAAA;gBAEpD,KAAK,CAAC,KAAK,IAAI,EAAE;oBACf,MAAM,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;oBAC/B,MAAM,QAAQ,CAAC,KAAK,EAAE,CAAA;gBACxB,CAAC,CAAC,EAAE,CAAA;YACN,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/interface-compliance-tests",
|
|
3
|
-
"version": "3.0.7-
|
|
3
|
+
"version": "3.0.7-e9cafd3d",
|
|
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/master/packages/interface-compliance-tests#readme",
|
|
@@ -44,13 +44,41 @@
|
|
|
44
44
|
"types": "./dist/src/index.d.ts",
|
|
45
45
|
"import": "./dist/src/index.js"
|
|
46
46
|
},
|
|
47
|
+
"./connection": {
|
|
48
|
+
"types": "./dist/src/connection/index.d.ts",
|
|
49
|
+
"import": "./dist/src/connection/index.js"
|
|
50
|
+
},
|
|
51
|
+
"./connection-encryption": {
|
|
52
|
+
"types": "./dist/src/connection-encryption/index.d.ts",
|
|
53
|
+
"import": "./dist/src/connection-encryption/index.js"
|
|
54
|
+
},
|
|
47
55
|
"./is-valid-tick": {
|
|
48
56
|
"types": "./dist/src/is-valid-tick.d.ts",
|
|
49
57
|
"import": "./dist/src/is-valid-tick.js"
|
|
50
58
|
},
|
|
59
|
+
"./mocks": {
|
|
60
|
+
"types": "./dist/src/mocks/index.d.ts",
|
|
61
|
+
"import": "./dist/src/mocks/index.js"
|
|
62
|
+
},
|
|
63
|
+
"./peer-discovery": {
|
|
64
|
+
"types": "./dist/src/peer-discovery/index.d.ts",
|
|
65
|
+
"import": "./dist/src/peer-discovery/index.js"
|
|
66
|
+
},
|
|
51
67
|
"./peers": {
|
|
52
68
|
"types": "./dist/src/peers.d.ts",
|
|
53
69
|
"import": "./dist/src/peers.js"
|
|
70
|
+
},
|
|
71
|
+
"./pubsub": {
|
|
72
|
+
"types": "./dist/src/pubsub/index.d.ts",
|
|
73
|
+
"import": "./dist/src/pubsub/index.js"
|
|
74
|
+
},
|
|
75
|
+
"./stream-muxer": {
|
|
76
|
+
"types": "./dist/src/stream-muxer/index.d.ts",
|
|
77
|
+
"import": "./dist/src/stream-muxer/index.js"
|
|
78
|
+
},
|
|
79
|
+
"./transport": {
|
|
80
|
+
"types": "./dist/src/transport/index.d.ts",
|
|
81
|
+
"import": "./dist/src/transport/index.js"
|
|
54
82
|
}
|
|
55
83
|
},
|
|
56
84
|
"eslintConfig": {
|
|
@@ -63,10 +91,48 @@
|
|
|
63
91
|
"clean": "aegir clean",
|
|
64
92
|
"lint": "aegir lint",
|
|
65
93
|
"dep-check": "aegir dep-check",
|
|
66
|
-
"build": "aegir build"
|
|
94
|
+
"build": "aegir build",
|
|
95
|
+
"test": "aegir test",
|
|
96
|
+
"test:chrome": "aegir test -t browser --cov",
|
|
97
|
+
"test:chrome-webworker": "aegir test -t webworker",
|
|
98
|
+
"test:firefox": "aegir test -t browser -- --browser firefox",
|
|
99
|
+
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
|
|
100
|
+
"test:node": "aegir test -t node --cov",
|
|
101
|
+
"test:electron-main": "aegir test -t electron-main"
|
|
67
102
|
},
|
|
68
103
|
"dependencies": {
|
|
69
|
-
"
|
|
104
|
+
"@libp2p/interface": "0.0.1-e9cafd3d",
|
|
105
|
+
"@libp2p/interface-internal": "0.0.1-e9cafd3d",
|
|
106
|
+
"@libp2p/logger": "2.1.1-e9cafd3d",
|
|
107
|
+
"@libp2p/multistream-select": "3.1.9-e9cafd3d",
|
|
108
|
+
"@libp2p/peer-collections": "3.0.2-e9cafd3d",
|
|
109
|
+
"@libp2p/peer-id": "2.0.3-e9cafd3d",
|
|
110
|
+
"@libp2p/peer-id-factory": "2.0.3-e9cafd3d",
|
|
111
|
+
"@multiformats/multiaddr": "^12.1.3",
|
|
112
|
+
"abortable-iterator": "^5.0.1",
|
|
113
|
+
"any-signal": "^4.1.1",
|
|
114
|
+
"delay": "^6.0.0",
|
|
115
|
+
"it-all": "^3.0.2",
|
|
116
|
+
"it-drain": "^3.0.2",
|
|
117
|
+
"it-handshake": "^4.1.3",
|
|
118
|
+
"it-map": "^3.0.3",
|
|
119
|
+
"it-ndjson": "^1.0.3",
|
|
120
|
+
"it-pair": "^2.0.6",
|
|
121
|
+
"it-pipe": "^3.0.1",
|
|
122
|
+
"it-pushable": "^3.1.3",
|
|
123
|
+
"it-stream-types": "^2.0.1",
|
|
124
|
+
"merge-options": "^3.0.4",
|
|
125
|
+
"p-defer": "^4.0.0",
|
|
126
|
+
"p-event": "^6.0.0",
|
|
127
|
+
"p-limit": "^4.0.0",
|
|
128
|
+
"p-wait-for": "^5.0.2",
|
|
129
|
+
"sinon": "^15.1.2",
|
|
130
|
+
"ts-sinon": "^2.0.2",
|
|
131
|
+
"uint8arraylist": "^2.4.3",
|
|
132
|
+
"uint8arrays": "^4.0.4"
|
|
133
|
+
},
|
|
134
|
+
"devDependencies": {
|
|
135
|
+
"aegir": "^39.0.13"
|
|
70
136
|
},
|
|
71
137
|
"typedoc": {
|
|
72
138
|
"entryPoint": "./src/index.ts"
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { expect } from 'aegir/chai'
|
|
2
|
+
import sinon from 'sinon'
|
|
3
|
+
import { stubInterface } from 'ts-sinon'
|
|
4
|
+
import type { TestSetup } from '../index.js'
|
|
5
|
+
import type { Connection, Stream } from '@libp2p/interface/connection'
|
|
6
|
+
|
|
7
|
+
export default (test: TestSetup<Connection>): void => {
|
|
8
|
+
describe('connection', () => {
|
|
9
|
+
describe('open connection', () => {
|
|
10
|
+
let connection: Connection
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
connection = await test.setup()
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
afterEach(async () => {
|
|
17
|
+
await connection.close()
|
|
18
|
+
await test.teardown()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('should have properties set', () => {
|
|
22
|
+
expect(connection.id).to.exist()
|
|
23
|
+
expect(connection.remotePeer).to.exist()
|
|
24
|
+
expect(connection.remoteAddr).to.exist()
|
|
25
|
+
expect(connection.status).to.equal('OPEN')
|
|
26
|
+
expect(connection.timeline.open).to.exist()
|
|
27
|
+
expect(connection.timeline.close).to.not.exist()
|
|
28
|
+
expect(connection.direction).to.exist()
|
|
29
|
+
expect(connection.streams).to.eql([])
|
|
30
|
+
expect(connection.tags).to.eql([])
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should get the metadata of an open connection', () => {
|
|
34
|
+
expect(connection.status).to.equal('OPEN')
|
|
35
|
+
expect(connection.direction).to.exist()
|
|
36
|
+
expect(connection.timeline.open).to.exist()
|
|
37
|
+
expect(connection.timeline.close).to.not.exist()
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('should return an empty array of streams', () => {
|
|
41
|
+
const streams = connection.streams
|
|
42
|
+
|
|
43
|
+
expect(streams).to.eql([])
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('should be able to create a new stream', async () => {
|
|
47
|
+
expect(connection.streams).to.be.empty()
|
|
48
|
+
|
|
49
|
+
const protocolToUse = '/echo/0.0.1'
|
|
50
|
+
const stream = await connection.newStream([protocolToUse])
|
|
51
|
+
|
|
52
|
+
expect(stream).to.have.property('protocol', protocolToUse)
|
|
53
|
+
|
|
54
|
+
const connStreams = connection.streams
|
|
55
|
+
|
|
56
|
+
expect(stream).to.exist()
|
|
57
|
+
expect(connStreams).to.exist()
|
|
58
|
+
expect(connStreams).to.have.lengthOf(1)
|
|
59
|
+
expect(connStreams[0]).to.equal(stream)
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
describe('close connection', () => {
|
|
64
|
+
let connection: Connection
|
|
65
|
+
let timelineProxy
|
|
66
|
+
const proxyHandler = {
|
|
67
|
+
set () {
|
|
68
|
+
// @ts-expect-error - TS fails to infer here
|
|
69
|
+
return Reflect.set(...arguments)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
beforeEach(async () => {
|
|
74
|
+
timelineProxy = new Proxy({
|
|
75
|
+
open: Date.now() - 10,
|
|
76
|
+
upgraded: Date.now()
|
|
77
|
+
}, proxyHandler)
|
|
78
|
+
|
|
79
|
+
connection = await test.setup()
|
|
80
|
+
connection.timeline = timelineProxy
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
afterEach(async () => {
|
|
84
|
+
await test.teardown()
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('should be able to close the connection after being created', async () => {
|
|
88
|
+
expect(connection.timeline.close).to.not.exist()
|
|
89
|
+
await connection.close()
|
|
90
|
+
|
|
91
|
+
expect(connection.timeline.close).to.exist()
|
|
92
|
+
expect(connection.status).to.equal('CLOSED')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('should be able to close the connection after opening a stream', async () => {
|
|
96
|
+
// Open stream
|
|
97
|
+
const protocol = '/echo/0.0.1'
|
|
98
|
+
await connection.newStream([protocol])
|
|
99
|
+
|
|
100
|
+
// Close connection
|
|
101
|
+
expect(connection.timeline.close).to.not.exist()
|
|
102
|
+
await connection.close()
|
|
103
|
+
|
|
104
|
+
expect(connection.timeline.close).to.exist()
|
|
105
|
+
expect(connection.status).to.equal('CLOSED')
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('should properly track streams', async () => {
|
|
109
|
+
// Open stream
|
|
110
|
+
const protocol = '/echo/0.0.1'
|
|
111
|
+
const stream = await connection.newStream([protocol])
|
|
112
|
+
expect(stream).to.have.property('protocol', protocol)
|
|
113
|
+
|
|
114
|
+
// Close stream
|
|
115
|
+
stream.close()
|
|
116
|
+
|
|
117
|
+
expect(connection.streams.filter(s => s.id === stream.id)).to.be.empty()
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it('should track outbound streams', async () => {
|
|
121
|
+
// Open stream
|
|
122
|
+
const protocol = '/echo/0.0.1'
|
|
123
|
+
const stream = await connection.newStream(protocol)
|
|
124
|
+
expect(stream).to.have.property('direction', 'outbound')
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it.skip('should track inbound streams', async () => {
|
|
128
|
+
// Add an remotely opened stream
|
|
129
|
+
const stream = stubInterface<Stream>()
|
|
130
|
+
connection.addStream(stream)
|
|
131
|
+
expect(stream).to.have.property('direction', 'inbound')
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('should support a proxy on the timeline', async () => {
|
|
135
|
+
sinon.spy(proxyHandler, 'set')
|
|
136
|
+
expect(connection.timeline.close).to.not.exist()
|
|
137
|
+
|
|
138
|
+
await connection.close()
|
|
139
|
+
// @ts-expect-error - fails to infer callCount
|
|
140
|
+
expect(proxyHandler.set.callCount).to.equal(1)
|
|
141
|
+
// @ts-expect-error - fails to infer getCall
|
|
142
|
+
const [obj, key, value] = proxyHandler.set.getCall(0).args
|
|
143
|
+
expect(obj).to.eql(connection.timeline)
|
|
144
|
+
expect(key).to.equal('close')
|
|
145
|
+
expect(value).to.be.a('number').that.equals(connection.timeline.close)
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('should fail to create a new stream if the connection is closing', async () => {
|
|
149
|
+
expect(connection.timeline.close).to.not.exist()
|
|
150
|
+
const p = connection.close()
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
const protocol = '/echo/0.0.1'
|
|
154
|
+
await connection.newStream([protocol])
|
|
155
|
+
} catch (err: any) {
|
|
156
|
+
expect(err).to.exist()
|
|
157
|
+
return
|
|
158
|
+
} finally {
|
|
159
|
+
await p
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
throw new Error('should fail to create a new stream if the connection is closing')
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('should fail to create a new stream if the connection is closed', async () => {
|
|
166
|
+
expect(connection.timeline.close).to.not.exist()
|
|
167
|
+
await connection.close()
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
const protocol = '/echo/0.0.1'
|
|
171
|
+
await connection.newStream([protocol])
|
|
172
|
+
} catch (err: any) {
|
|
173
|
+
expect(err).to.exist()
|
|
174
|
+
expect(err.code).to.equal('ERR_CONNECTION_CLOSED')
|
|
175
|
+
return
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
throw new Error('should fail to create a new stream if the connection is closing')
|
|
179
|
+
})
|
|
180
|
+
})
|
|
181
|
+
})
|
|
182
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
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
|
+
import type { TestSetup } from '../index.js'
|
|
10
|
+
import type { ConnectionEncrypter } from '@libp2p/interface/connection-encrypter'
|
|
11
|
+
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
12
|
+
|
|
13
|
+
export default (common: TestSetup<ConnectionEncrypter>): void => {
|
|
14
|
+
describe('interface-connection-encrypter compliance tests', () => {
|
|
15
|
+
let crypto: ConnectionEncrypter
|
|
16
|
+
let localPeer: PeerId
|
|
17
|
+
let remotePeer: PeerId
|
|
18
|
+
let mitmPeer: PeerId
|
|
19
|
+
|
|
20
|
+
before(async () => {
|
|
21
|
+
[
|
|
22
|
+
crypto,
|
|
23
|
+
localPeer,
|
|
24
|
+
remotePeer,
|
|
25
|
+
mitmPeer
|
|
26
|
+
] = await Promise.all([
|
|
27
|
+
common.setup(),
|
|
28
|
+
PeerIdFactory.createFromJSON(peers[0]),
|
|
29
|
+
PeerIdFactory.createFromJSON(peers[1]),
|
|
30
|
+
PeerIdFactory.createFromJSON(peers[2])
|
|
31
|
+
])
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
after(async () => {
|
|
35
|
+
await common.teardown()
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('has a protocol string', () => {
|
|
39
|
+
expect(crypto.protocol).to.exist()
|
|
40
|
+
expect(crypto.protocol).to.be.a('string')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('it wraps the provided duplex connection', async () => {
|
|
44
|
+
const [localConn, remoteConn] = createMaConnPair()
|
|
45
|
+
|
|
46
|
+
const [
|
|
47
|
+
inboundResult,
|
|
48
|
+
outboundResult
|
|
49
|
+
] = await Promise.all([
|
|
50
|
+
crypto.secureInbound(remotePeer, localConn),
|
|
51
|
+
crypto.secureOutbound(localPeer, remoteConn, remotePeer)
|
|
52
|
+
])
|
|
53
|
+
|
|
54
|
+
// Echo server
|
|
55
|
+
void pipe(inboundResult.conn, inboundResult.conn)
|
|
56
|
+
|
|
57
|
+
// Send some data and collect the result
|
|
58
|
+
const input = uint8ArrayFromString('data to encrypt')
|
|
59
|
+
const result = await pipe(
|
|
60
|
+
[input],
|
|
61
|
+
outboundResult.conn,
|
|
62
|
+
async (source) => all(source)
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
expect(result).to.eql([input])
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('should return the remote peer id', async () => {
|
|
69
|
+
const [localConn, remoteConn] = createMaConnPair()
|
|
70
|
+
|
|
71
|
+
const [
|
|
72
|
+
inboundResult,
|
|
73
|
+
outboundResult
|
|
74
|
+
] = await Promise.all([
|
|
75
|
+
crypto.secureInbound(remotePeer, localConn),
|
|
76
|
+
crypto.secureOutbound(localPeer, remoteConn, remotePeer)
|
|
77
|
+
])
|
|
78
|
+
|
|
79
|
+
// Inbound should return the initiator (local) peer
|
|
80
|
+
expect(inboundResult.remotePeer.toBytes()).to.equalBytes(localPeer.toBytes())
|
|
81
|
+
// Outbound should return the receiver (remote) peer
|
|
82
|
+
expect(outboundResult.remotePeer.toBytes()).to.equalBytes(remotePeer.toBytes())
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('inbound connections should verify peer integrity if known', async () => {
|
|
86
|
+
const [localConn, remoteConn] = createMaConnPair()
|
|
87
|
+
|
|
88
|
+
await Promise.all([
|
|
89
|
+
crypto.secureInbound(remotePeer, localConn, mitmPeer),
|
|
90
|
+
crypto.secureOutbound(localPeer, remoteConn, remotePeer)
|
|
91
|
+
]).then(() => expect.fail(), (err) => {
|
|
92
|
+
expect(err).to.exist()
|
|
93
|
+
expect(err).to.have.property('code', UnexpectedPeerError.code)
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { multiaddr } from '@multiformats/multiaddr'
|
|
2
|
+
import { duplexPair } from 'it-pair/duplex'
|
|
3
|
+
import type { MultiaddrConnection } from '@libp2p/interface/connection'
|
|
4
|
+
import type { Duplex, Source } from 'it-stream-types'
|
|
5
|
+
|
|
6
|
+
export function createMaConnPair (): [MultiaddrConnection, MultiaddrConnection] {
|
|
7
|
+
const [local, remote] = duplexPair<Uint8Array>()
|
|
8
|
+
|
|
9
|
+
function duplexToMaConn (duplex: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>): MultiaddrConnection {
|
|
10
|
+
const output: MultiaddrConnection = {
|
|
11
|
+
...duplex,
|
|
12
|
+
close: async () => {},
|
|
13
|
+
remoteAddr: multiaddr('/ip4/127.0.0.1/tcp/4001'),
|
|
14
|
+
timeline: {
|
|
15
|
+
open: Date.now()
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return output
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return [duplexToMaConn(local), duplexToMaConn(remote)]
|
|
23
|
+
}
|