@libp2p/interface-compliance-tests 6.5.0-8484de8a2 → 6.5.0-87bc8d4fb
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/connection-encryption/index.d.ts.map +1 -1
- package/dist/src/connection-encryption/index.js +15 -24
- package/dist/src/connection-encryption/index.js.map +1 -1
- 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 +21 -0
- package/dist/src/connection-encryption/utils/index.js.map +1 -0
- package/dist/src/matchers.d.ts +12 -0
- package/dist/src/matchers.d.ts.map +1 -0
- package/dist/src/matchers.js +14 -0
- package/dist/src/matchers.js.map +1 -0
- package/dist/src/mocks/connection-manager.d.ts +27 -0
- package/dist/src/mocks/connection-manager.d.ts.map +1 -0
- package/dist/src/mocks/connection-manager.js +147 -0
- package/dist/src/mocks/connection-manager.js.map +1 -0
- package/dist/src/mocks/connection.d.ts +41 -0
- package/dist/src/mocks/connection.d.ts.map +1 -0
- package/dist/src/mocks/connection.js +236 -0
- package/dist/src/mocks/connection.js.map +1 -0
- package/dist/src/mocks/duplex.d.ts +4 -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 +12 -0
- package/dist/src/mocks/index.d.ts.map +1 -0
- package/dist/src/mocks/index.js +8 -0
- package/dist/src/mocks/index.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 +64 -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 +234 -0
- package/dist/src/mocks/muxer.js.map +1 -0
- package/dist/src/mocks/registrar.d.ts +16 -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 +9 -0
- package/dist/src/mocks/upgrader.d.ts.map +1 -0
- package/dist/src/mocks/upgrader.js +46 -0
- package/dist/src/mocks/upgrader.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 +88 -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 +216 -0
- package/dist/src/pubsub/two-nodes.js.map +1 -0
- package/dist/src/pubsub/utils.d.ts +5 -0
- package/dist/src/pubsub/utils.d.ts.map +1 -0
- package/dist/src/pubsub/utils.js +27 -0
- package/dist/src/pubsub/utils.js.map +1 -0
- package/dist/src/stream-muxer/base-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/base-test.js +345 -62
- package/dist/src/stream-muxer/base-test.js.map +1 -1
- package/dist/src/stream-muxer/close-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/close-test.js +320 -253
- package/dist/src/stream-muxer/close-test.js.map +1 -1
- package/dist/src/stream-muxer/index.js +2 -2
- package/dist/src/stream-muxer/index.js.map +1 -1
- package/dist/src/stream-muxer/{stream-test.d.ts → mega-stress-test.d.ts} +2 -2
- 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 +39 -0
- package/dist/src/stream-muxer/spawner.js.map +1 -0
- package/dist/src/stream-muxer/stress-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/stress-test.js +16 -70
- package/dist/src/stream-muxer/stress-test.js.map +1 -1
- package/dist/src/transport/index.d.ts.map +1 -1
- package/dist/src/transport/index.js +203 -232
- package/dist/src/transport/index.js.map +1 -1
- package/dist/src/transport/utils.js +2 -2
- package/dist/src/transport/utils.js.map +1 -1
- package/package.json +51 -23
- package/src/connection-encryption/index.ts +20 -27
- package/src/connection-encryption/utils/index.ts +27 -0
- package/src/matchers.ts +18 -0
- package/src/mocks/connection-manager.ts +216 -0
- package/src/mocks/connection.ts +309 -0
- package/src/mocks/duplex.ts +11 -0
- package/src/mocks/index.ts +11 -0
- package/src/mocks/multiaddr-connection.ts +80 -0
- package/src/mocks/muxer.ts +331 -0
- package/src/mocks/registrar.ts +86 -0
- package/src/mocks/upgrader.ts +65 -0
- package/src/pubsub/api.ts +116 -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 +272 -0
- package/src/pubsub/utils.ts +34 -0
- package/src/stream-muxer/base-test.ts +413 -75
- package/src/stream-muxer/close-test.ts +343 -304
- package/src/stream-muxer/index.ts +2 -2
- package/src/stream-muxer/mega-stress-test.ts +14 -0
- package/src/stream-muxer/spawner.ts +57 -0
- package/src/stream-muxer/stress-test.ts +18 -92
- package/src/transport/index.ts +241 -280
- package/src/transport/utils.ts +2 -2
- package/dist/src/stream-muxer/stream-test.d.ts.map +0 -1
- package/dist/src/stream-muxer/stream-test.js +0 -289
- package/dist/src/stream-muxer/stream-test.js.map +0 -1
- package/src/stream-muxer/stream-test.ts +0 -380
|
@@ -1 +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,EAAU,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAEhF,MAAM,WAAW,4BAA4B;IAC3C,UAAU,EAAE,UAAU,CAAA;CACvB;yBAEe,QAAQ,SAAS,CAAC,mBAAmB,EAAE,4BAA4B,CAAC,KAAG,IAAI;AAA3F,
|
|
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,EAAU,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAEhF,MAAM,WAAW,4BAA4B;IAC3C,UAAU,EAAE,UAAU,CAAA;CACvB;yBAEe,QAAQ,SAAS,CAAC,mBAAmB,EAAE,4BAA4B,CAAC,KAAG,IAAI;AAA3F,wBAoGC"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { generateKeyPair } from '@libp2p/crypto/keys';
|
|
2
2
|
import { peerIdFromPrivateKey } from '@libp2p/peer-id';
|
|
3
|
-
import { multiaddrConnectionPair, echo } from '@libp2p/utils';
|
|
4
3
|
import { expect } from 'aegir/chai';
|
|
5
4
|
import all from 'it-all';
|
|
5
|
+
import { pipe } from 'it-pipe';
|
|
6
6
|
import toBuffer from 'it-to-buffer';
|
|
7
|
-
import { pEvent } from 'p-event';
|
|
8
7
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
8
|
+
import { createMaConnPair } from './utils/index.js';
|
|
9
9
|
export default (common) => {
|
|
10
10
|
describe('interface-connection-encrypter compliance tests', () => {
|
|
11
11
|
let crypto;
|
|
@@ -34,35 +34,26 @@ export default (common) => {
|
|
|
34
34
|
expect(crypto.protocol).to.be.a('string');
|
|
35
35
|
});
|
|
36
36
|
it('it wraps the provided duplex connection', async () => {
|
|
37
|
-
const [localConn, remoteConn] =
|
|
37
|
+
const [localConn, remoteConn] = createMaConnPair();
|
|
38
38
|
const [inboundResult, outboundResult] = await Promise.all([
|
|
39
|
-
cryptoRemote.secureInbound(
|
|
40
|
-
crypto.secureOutbound(
|
|
39
|
+
cryptoRemote.secureInbound(localConn),
|
|
40
|
+
crypto.secureOutbound(remoteConn, {
|
|
41
41
|
remotePeer
|
|
42
42
|
})
|
|
43
43
|
]);
|
|
44
44
|
// Echo server
|
|
45
|
-
|
|
46
|
-
const input =
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
45
|
+
void pipe(inboundResult.conn, inboundResult.conn);
|
|
46
|
+
const input = new Array(10_000).fill(0).map((val, index) => {
|
|
47
|
+
return uint8ArrayFromString(`data to encrypt, chunk ${index}`);
|
|
48
|
+
});
|
|
50
49
|
// Send some data and collect the result
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (!outboundResult.connection.send(buf)) {
|
|
56
|
-
await pEvent(outboundResult.connection, 'drain');
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
await outboundResult.connection.close();
|
|
60
|
-
})
|
|
61
|
-
]);
|
|
62
|
-
expect(toBuffer(output.map(b => b.subarray()))).to.equalBytes(toBuffer(input));
|
|
50
|
+
const result = await pipe(async function* () {
|
|
51
|
+
yield* input;
|
|
52
|
+
}, outboundResult.conn, async (source) => all(source));
|
|
53
|
+
expect(toBuffer(result.map(b => b.subarray()))).to.equalBytes(toBuffer(input));
|
|
63
54
|
});
|
|
64
55
|
it('should return the remote peer id', async () => {
|
|
65
|
-
const [
|
|
56
|
+
const [localConn, remoteConn] = createMaConnPair();
|
|
66
57
|
const [inboundResult, outboundResult] = await Promise.all([
|
|
67
58
|
cryptoRemote.secureInbound(localConn),
|
|
68
59
|
crypto.secureOutbound(remoteConn, {
|
|
@@ -75,7 +66,7 @@ export default (common) => {
|
|
|
75
66
|
expect(outboundResult.remotePeer.toString()).to.equal(remotePeer.toString());
|
|
76
67
|
});
|
|
77
68
|
it('inbound connections should verify peer integrity if known', async () => {
|
|
78
|
-
const [
|
|
69
|
+
const [localConn, remoteConn] = createMaConnPair();
|
|
79
70
|
await Promise.all([
|
|
80
71
|
cryptoRemote.secureInbound(localConn, {
|
|
81
72
|
remotePeer: mitmPeer
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/connection-encryption/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAA;AACtD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/connection-encryption/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,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CAAC,CAAA;YAC7D,CAAC,CAAC,CAAA;QACJ,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/utils/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAI5D,wBAAgB,gBAAgB,IAAK,CAAC,mBAAmB,EAAE,mBAAmB,CAAC,CAmB9E"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger';
|
|
2
|
+
import { multiaddr } from '@multiformats/multiaddr';
|
|
3
|
+
import { duplexPair } from 'it-pair/duplex';
|
|
4
|
+
export function createMaConnPair() {
|
|
5
|
+
const [local, remote] = duplexPair();
|
|
6
|
+
function duplexToMaConn(duplex) {
|
|
7
|
+
const output = {
|
|
8
|
+
...duplex,
|
|
9
|
+
close: async () => { },
|
|
10
|
+
abort: () => { },
|
|
11
|
+
remoteAddr: multiaddr('/ip4/127.0.0.1/tcp/4001'),
|
|
12
|
+
timeline: {
|
|
13
|
+
open: Date.now()
|
|
14
|
+
},
|
|
15
|
+
log: logger('duplex-maconn')
|
|
16
|
+
};
|
|
17
|
+
return output;
|
|
18
|
+
}
|
|
19
|
+
return [duplexToMaConn(local), duplexToMaConn(remote)];
|
|
20
|
+
}
|
|
21
|
+
//# 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,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAK3C,MAAM,UAAU,gBAAgB;IAC9B,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,UAAU,EAA+B,CAAA;IAEjE,SAAS,cAAc,CAAE,MAA+G;QACtI,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;YACD,GAAG,EAAE,MAAM,CAAC,eAAe,CAAC;SAC7B,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,12 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interface';
|
|
2
|
+
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
3
|
+
import type { SinonMatcher } from 'sinon';
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated PeerIds can be passed to sinon matchers directly
|
|
6
|
+
*/
|
|
7
|
+
export declare function matchPeerId(peerId: PeerId): SinonMatcher;
|
|
8
|
+
/**
|
|
9
|
+
* @deprecated Multiaddrs can be passed to sinon matchers directly
|
|
10
|
+
*/
|
|
11
|
+
export declare function matchMultiaddr(ma: Multiaddr): SinonMatcher;
|
|
12
|
+
//# sourceMappingURL=matchers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matchers.d.ts","sourceRoot":"","sources":["../../src/matchers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAA;AAEzC;;GAEG;AACH,wBAAgB,WAAW,CAAE,MAAM,EAAE,MAAM,GAAG,YAAY,CAEzD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAE,EAAE,EAAE,SAAS,GAAG,YAAY,CAE3D"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Sinon from 'sinon';
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated PeerIds can be passed to sinon matchers directly
|
|
4
|
+
*/
|
|
5
|
+
export function matchPeerId(peerId) {
|
|
6
|
+
return Sinon.match(p => p.toString() === peerId.toString());
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* @deprecated Multiaddrs can be passed to sinon matchers directly
|
|
10
|
+
*/
|
|
11
|
+
export function matchMultiaddr(ma) {
|
|
12
|
+
return Sinon.match(m => m.toString() === ma.toString());
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=matchers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matchers.js","sourceRoot":"","sources":["../../src/matchers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAKzB;;GAEG;AACH,MAAM,UAAU,WAAW,CAAE,MAAc;IACzC,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,EAAE,KAAK,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAA;AAC7D,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc,CAAE,EAAa;IAC3C,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,EAAE,KAAK,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAA;AACzD,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { PrivateKey, PeerId, ComponentLogger, Libp2pEvents, PubSub } from '@libp2p/interface';
|
|
2
|
+
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal';
|
|
3
|
+
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
4
|
+
import type { TypedEventTarget } from 'main-event';
|
|
5
|
+
export interface MockNetworkComponents {
|
|
6
|
+
peerId: PeerId;
|
|
7
|
+
privateKey: PrivateKey;
|
|
8
|
+
registrar: Registrar;
|
|
9
|
+
connectionManager: ConnectionManager;
|
|
10
|
+
events: TypedEventTarget<Libp2pEvents>;
|
|
11
|
+
pubsub?: PubSub;
|
|
12
|
+
logger: ComponentLogger;
|
|
13
|
+
}
|
|
14
|
+
export declare class MockNetwork {
|
|
15
|
+
private components;
|
|
16
|
+
addNode(components: MockNetworkComponents): void;
|
|
17
|
+
getNode(peerId: PeerId | Multiaddr[]): MockNetworkComponents;
|
|
18
|
+
reset(): void;
|
|
19
|
+
}
|
|
20
|
+
export declare const mockNetwork: MockNetwork;
|
|
21
|
+
export interface MockConnectionManagerComponents {
|
|
22
|
+
peerId: PeerId;
|
|
23
|
+
registrar: Registrar;
|
|
24
|
+
events: TypedEventTarget<Libp2pEvents>;
|
|
25
|
+
}
|
|
26
|
+
export declare function mockConnectionManager(components: MockConnectionManagerComponents): ConnectionManager;
|
|
27
|
+
//# 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":"AAKA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,eAAe,EAAE,YAAY,EAA2B,MAAM,EAAa,MAAM,mBAAmB,CAAA;AACtI,OAAO,KAAK,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAA;AAC9E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAElD,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,UAAU,CAAA;IACtB,SAAS,EAAE,SAAS,CAAA;IACpB,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAA;IACtC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,qBAAa,WAAW;IACtB,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,gBAAgB,CAAC,YAAY,CAAC,CAAA;CACvC;AAgKD,wBAAgB,qBAAqB,CAAE,UAAU,EAAE,+BAA+B,GAAG,iBAAiB,CAErG"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { UnsupportedOperationError, isPeerId } from '@libp2p/interface';
|
|
2
|
+
import { PeerMap } from '@libp2p/peer-collections';
|
|
3
|
+
import { peerIdFromString } from '@libp2p/peer-id';
|
|
4
|
+
import { isMultiaddr } from '@multiformats/multiaddr';
|
|
5
|
+
import { connectionPair } from './connection.js';
|
|
6
|
+
export class MockNetwork {
|
|
7
|
+
components = [];
|
|
8
|
+
addNode(components) {
|
|
9
|
+
this.components.push(components);
|
|
10
|
+
}
|
|
11
|
+
getNode(peerId) {
|
|
12
|
+
if (Array.isArray(peerId) && peerId.length > 0) {
|
|
13
|
+
peerId = peerIdFromString(peerId[0].getPeerId() ?? '');
|
|
14
|
+
}
|
|
15
|
+
else if (isPeerId(peerId)) {
|
|
16
|
+
for (const components of this.components) {
|
|
17
|
+
if (peerId.equals(components.peerId)) {
|
|
18
|
+
return components;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
throw new Error('Peer not found');
|
|
23
|
+
}
|
|
24
|
+
reset() {
|
|
25
|
+
this.components = [];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export const mockNetwork = new MockNetwork();
|
|
29
|
+
class MockConnectionManager {
|
|
30
|
+
connections = [];
|
|
31
|
+
components;
|
|
32
|
+
started = false;
|
|
33
|
+
constructor(components) {
|
|
34
|
+
this.components = components;
|
|
35
|
+
}
|
|
36
|
+
isStarted() {
|
|
37
|
+
return this.started;
|
|
38
|
+
}
|
|
39
|
+
async start() {
|
|
40
|
+
this.started = true;
|
|
41
|
+
}
|
|
42
|
+
async stop() {
|
|
43
|
+
for (const connection of this.connections) {
|
|
44
|
+
await this.closeConnections(connection.remotePeer);
|
|
45
|
+
}
|
|
46
|
+
this.started = false;
|
|
47
|
+
}
|
|
48
|
+
getConnections(peerId) {
|
|
49
|
+
if (peerId != null) {
|
|
50
|
+
return this.connections
|
|
51
|
+
.filter(c => c.remotePeer.toString() === peerId.toString());
|
|
52
|
+
}
|
|
53
|
+
return this.connections;
|
|
54
|
+
}
|
|
55
|
+
getConnectionsMap() {
|
|
56
|
+
const map = new PeerMap();
|
|
57
|
+
for (const conn of this.connections) {
|
|
58
|
+
const conns = map.get(conn.remotePeer) ?? [];
|
|
59
|
+
conns.push(conn);
|
|
60
|
+
map.set(conn.remotePeer, conns);
|
|
61
|
+
}
|
|
62
|
+
return map;
|
|
63
|
+
}
|
|
64
|
+
getMaxConnections() {
|
|
65
|
+
return 10_000;
|
|
66
|
+
}
|
|
67
|
+
setMaxConnections() {
|
|
68
|
+
}
|
|
69
|
+
async openConnection(peerId, options) {
|
|
70
|
+
if (isMultiaddr(peerId)) {
|
|
71
|
+
throw new UnsupportedOperationError('Dialing multiaddrs not supported');
|
|
72
|
+
}
|
|
73
|
+
options?.signal?.throwIfAborted();
|
|
74
|
+
let existingConnections = [];
|
|
75
|
+
if (Array.isArray(peerId) && peerId.length > 0) {
|
|
76
|
+
existingConnections = this.getConnections(peerIdFromString(peerId[0].getPeerId() ?? ''));
|
|
77
|
+
}
|
|
78
|
+
else if (isPeerId(peerId)) {
|
|
79
|
+
existingConnections = this.getConnections(peerId);
|
|
80
|
+
}
|
|
81
|
+
if (existingConnections.length > 0) {
|
|
82
|
+
return existingConnections[0];
|
|
83
|
+
}
|
|
84
|
+
const componentsB = mockNetwork.getNode(peerId);
|
|
85
|
+
const [aToB, bToA] = connectionPair(this.components, componentsB);
|
|
86
|
+
// track connections
|
|
87
|
+
this.connections.push(aToB);
|
|
88
|
+
componentsB.connectionManager.connections.push(bToA);
|
|
89
|
+
this.components.events.safeDispatchEvent('connection:open', {
|
|
90
|
+
detail: aToB
|
|
91
|
+
});
|
|
92
|
+
for (const protocol of this.components.registrar.getProtocols()) {
|
|
93
|
+
for (const topology of this.components.registrar.getTopologies(protocol)) {
|
|
94
|
+
topology.onConnect?.(componentsB.peerId, aToB);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
this.components.events.safeDispatchEvent('peer:connect', { detail: componentsB.peerId });
|
|
98
|
+
componentsB.events.safeDispatchEvent('connection:open', {
|
|
99
|
+
detail: bToA
|
|
100
|
+
});
|
|
101
|
+
for (const protocol of componentsB.registrar.getProtocols()) {
|
|
102
|
+
for (const topology of componentsB.registrar.getTopologies(protocol)) {
|
|
103
|
+
topology.onConnect?.(this.components.peerId, bToA);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
componentsB.events.safeDispatchEvent('peer:connect', { detail: this.components.peerId });
|
|
107
|
+
return aToB;
|
|
108
|
+
}
|
|
109
|
+
async closeConnections(peerId) {
|
|
110
|
+
const connections = this.getConnections(peerId);
|
|
111
|
+
if (connections.length === 0) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const componentsB = mockNetwork.getNode(peerId);
|
|
115
|
+
for (const protocol of this.components.registrar.getProtocols()) {
|
|
116
|
+
this.components.registrar.getTopologies(protocol).forEach(topology => {
|
|
117
|
+
topology.onDisconnect?.(componentsB.peerId);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
for (const conn of connections) {
|
|
121
|
+
await conn.close();
|
|
122
|
+
}
|
|
123
|
+
this.connections = this.connections.filter(c => !c.remotePeer.equals(peerId));
|
|
124
|
+
if (this.connections.filter(c => !c.remotePeer.equals(peerId)).length === 0) {
|
|
125
|
+
componentsB.events.safeDispatchEvent('peer:disconnect', { detail: peerId });
|
|
126
|
+
}
|
|
127
|
+
await componentsB.connectionManager?.closeConnections(this.components.peerId);
|
|
128
|
+
if (componentsB.connectionManager?.getConnectionsMap().get(this.components.peerId) == null) {
|
|
129
|
+
componentsB.events.safeDispatchEvent('peer:disconnect', { detail: this.components.peerId });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async acceptIncomingConnection() {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
afterUpgradeInbound() {
|
|
136
|
+
}
|
|
137
|
+
getDialQueue() {
|
|
138
|
+
return [];
|
|
139
|
+
}
|
|
140
|
+
async isDialable() {
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
export function mockConnectionManager(components) {
|
|
145
|
+
return new MockConnectionManager(components);
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=connection-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection-manager.js","sourceRoot":"","sources":["../../../src/mocks/connection-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACvE,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAiBhD,MAAM,OAAO,WAAW;IACd,UAAU,GAA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AAC,CAAC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAA;QAE7E,IAAI,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC5E,WAAW,CAAC,MAAM,CAAC,iBAAiB,CAAC,iBAAiB,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAA;QAC7E,CAAC;QAED,MAAM,WAAW,CAAC,iBAAiB,EAAE,gBAAgB,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;QAE7E,IAAI,WAAW,CAAC,iBAAiB,EAAE,iBAAiB,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;YAC3F,WAAW,CAAC,MAAM,CAAC,iBAAiB,CAAC,iBAAiB,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,CAAA;QAC7F,CAAC;IACH,CAAC;IAED,KAAK,CAAC,wBAAwB;QAC5B,OAAO,IAAI,CAAA;IACb,CAAC;IAED,mBAAmB;IAEnB,CAAC;IAED,YAAY;QACV,OAAO,EAAE,CAAA;IACX,CAAC;IAED,KAAK,CAAC,UAAU;QACd,OAAO,IAAI,CAAA;IACb,CAAC;CACF;AAED,MAAM,UAAU,qBAAqB,CAAE,UAA2C;IAChF,OAAO,IAAI,qBAAqB,CAAC,UAAU,CAAC,CAAA;AAC9C,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
2
|
+
import type { ComponentLogger, MultiaddrConnection, Connection, Stream, Direction, PeerId, StreamMuxerFactory } from '@libp2p/interface';
|
|
3
|
+
import type { Registrar } from '@libp2p/interface-internal';
|
|
4
|
+
import type { Duplex, Source } from 'it-stream-types';
|
|
5
|
+
export interface MockConnectionOptions {
|
|
6
|
+
direction?: Direction;
|
|
7
|
+
registrar?: Registrar;
|
|
8
|
+
muxerFactory?: StreamMuxerFactory;
|
|
9
|
+
logger?: ComponentLogger;
|
|
10
|
+
}
|
|
11
|
+
export declare function mockConnection(maConn: MultiaddrConnection, opts?: MockConnectionOptions): Connection;
|
|
12
|
+
export interface StreamInit {
|
|
13
|
+
direction?: Direction;
|
|
14
|
+
protocol?: string;
|
|
15
|
+
id?: string;
|
|
16
|
+
}
|
|
17
|
+
export declare function mockStream(stream: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>, init?: StreamInit): Stream;
|
|
18
|
+
export interface StreamPairInit {
|
|
19
|
+
duplex: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>;
|
|
20
|
+
init?: StreamInit;
|
|
21
|
+
}
|
|
22
|
+
export declare function streamPair(a: StreamPairInit, b: StreamPairInit, init?: StreamInit): [Stream, Stream];
|
|
23
|
+
export interface Peer {
|
|
24
|
+
peerId: PeerId;
|
|
25
|
+
registrar: Registrar;
|
|
26
|
+
}
|
|
27
|
+
export declare function multiaddrConnectionPair(a: {
|
|
28
|
+
peerId: PeerId;
|
|
29
|
+
registrar: Registrar;
|
|
30
|
+
}, b: {
|
|
31
|
+
peerId: PeerId;
|
|
32
|
+
registrar: Registrar;
|
|
33
|
+
}): [MultiaddrConnection, MultiaddrConnection];
|
|
34
|
+
export declare function connectionPair(a: {
|
|
35
|
+
peerId: PeerId;
|
|
36
|
+
registrar: Registrar;
|
|
37
|
+
}, b: {
|
|
38
|
+
peerId: PeerId;
|
|
39
|
+
registrar: Registrar;
|
|
40
|
+
}): [Connection, Connection];
|
|
41
|
+
//# sourceMappingURL=connection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../../src/moc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GAAE,UAAe,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAazG;AAED,MAAM,WAAW,IAAI;IACnB,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,EAAE,SAAS,CAAA;CACrB;AAED,wBAAgB,uBAAuB,CAAE,CAAC,EAAE;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,SAAS,CAAA;CAAE,EAAE,CAAC,EAAE;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,SAAS,CAAA;CAAE,GAAG,CAAE,mBAAmB,EAAE,mBAAmB,CAAE,CAO/K;AAED,wBAAgB,cAAc,CAAE,CAAC,EAAE;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,SAAS,CAAA;CAAE,EAAE,CAAC,EAAE;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,SAAS,CAAA;CAAE,GAAG,CAAE,UAAU,EAAE,UAAU,CAAE,CAWpJ"}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { ConnectionClosedError } from '@libp2p/interface';
|
|
2
|
+
import { defaultLogger, logger } from '@libp2p/logger';
|
|
3
|
+
import * as mss from '@libp2p/multistream-select';
|
|
4
|
+
import { peerIdFromString } from '@libp2p/peer-id';
|
|
5
|
+
import { closeSource } from '@libp2p/utils/close-source';
|
|
6
|
+
import { duplexPair } from 'it-pair/duplex';
|
|
7
|
+
import { pipe } from 'it-pipe';
|
|
8
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
9
|
+
import { mockMultiaddrConnection } from './multiaddr-connection.js';
|
|
10
|
+
import { mockMuxer } from './muxer.js';
|
|
11
|
+
import { mockRegistrar } from './registrar.js';
|
|
12
|
+
class MockConnection {
|
|
13
|
+
id;
|
|
14
|
+
remoteAddr;
|
|
15
|
+
remotePeer;
|
|
16
|
+
direction;
|
|
17
|
+
timeline;
|
|
18
|
+
multiplexer;
|
|
19
|
+
encryption;
|
|
20
|
+
status;
|
|
21
|
+
streams;
|
|
22
|
+
tags;
|
|
23
|
+
limits;
|
|
24
|
+
log;
|
|
25
|
+
muxer;
|
|
26
|
+
maConn;
|
|
27
|
+
logger;
|
|
28
|
+
constructor(init) {
|
|
29
|
+
const { remoteAddr, remotePeer, direction, maConn, muxer, logger } = init;
|
|
30
|
+
this.id = `mock-connection-${Math.random()}`;
|
|
31
|
+
this.remoteAddr = remoteAddr;
|
|
32
|
+
this.remotePeer = remotePeer;
|
|
33
|
+
this.direction = direction;
|
|
34
|
+
this.status = 'open';
|
|
35
|
+
this.direction = direction;
|
|
36
|
+
this.timeline = maConn.timeline;
|
|
37
|
+
this.multiplexer = 'test-multiplexer';
|
|
38
|
+
this.encryption = 'yes-yes-very-secure';
|
|
39
|
+
this.streams = [];
|
|
40
|
+
this.tags = [];
|
|
41
|
+
this.muxer = muxer;
|
|
42
|
+
this.maConn = maConn;
|
|
43
|
+
this.logger = logger;
|
|
44
|
+
this.log = logger.forComponent(this.id);
|
|
45
|
+
}
|
|
46
|
+
async newStream(protocols, options) {
|
|
47
|
+
if (!Array.isArray(protocols)) {
|
|
48
|
+
protocols = [protocols];
|
|
49
|
+
}
|
|
50
|
+
if (protocols.length === 0) {
|
|
51
|
+
throw new Error('protocols must have a length');
|
|
52
|
+
}
|
|
53
|
+
if (this.status !== 'open') {
|
|
54
|
+
throw new ConnectionClosedError('connection must be open to create streams');
|
|
55
|
+
}
|
|
56
|
+
options?.signal?.throwIfAborted();
|
|
57
|
+
const id = `${Math.random()}`;
|
|
58
|
+
const stream = await this.muxer.newStream(id);
|
|
59
|
+
const result = await mss.select(stream, protocols, {
|
|
60
|
+
...options,
|
|
61
|
+
log: this.logger.forComponent('libp2p:mock-connection:stream:mss:select')
|
|
62
|
+
});
|
|
63
|
+
stream.protocol = result.protocol;
|
|
64
|
+
stream.direction = 'outbound';
|
|
65
|
+
stream.sink = result.stream.sink;
|
|
66
|
+
stream.source = result.stream.source;
|
|
67
|
+
this.streams.push(stream);
|
|
68
|
+
return stream;
|
|
69
|
+
}
|
|
70
|
+
async close(options) {
|
|
71
|
+
this.status = 'closing';
|
|
72
|
+
await Promise.all(this.streams.map(async (s) => s.close(options)));
|
|
73
|
+
await this.maConn.close();
|
|
74
|
+
this.status = 'closed';
|
|
75
|
+
this.timeline.close = Date.now();
|
|
76
|
+
}
|
|
77
|
+
abort(err) {
|
|
78
|
+
this.status = 'closing';
|
|
79
|
+
this.streams.forEach(s => {
|
|
80
|
+
s.abort(err);
|
|
81
|
+
});
|
|
82
|
+
this.maConn.abort(err);
|
|
83
|
+
this.status = 'closed';
|
|
84
|
+
this.timeline.close = Date.now();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
export function mockConnection(maConn, opts = {}) {
|
|
88
|
+
const remoteAddr = maConn.remoteAddr;
|
|
89
|
+
const remotePeerIdStr = remoteAddr.getPeerId() ?? '12D3KooWCrhmFM1BCPGBkNzbPfDk4cjYmtAYSpZwUBC69Qg2kZyq';
|
|
90
|
+
const logger = opts.logger ?? defaultLogger();
|
|
91
|
+
if (remotePeerIdStr == null) {
|
|
92
|
+
throw new Error('Remote multiaddr must contain a peer id');
|
|
93
|
+
}
|
|
94
|
+
const remotePeer = peerIdFromString(remotePeerIdStr);
|
|
95
|
+
const direction = opts.direction ?? 'inbound';
|
|
96
|
+
const registrar = opts.registrar ?? mockRegistrar();
|
|
97
|
+
const muxerFactory = opts.muxerFactory ?? mockMuxer();
|
|
98
|
+
const log = logger.forComponent('libp2p:mock-muxer');
|
|
99
|
+
const muxer = muxerFactory.createStreamMuxer({
|
|
100
|
+
log,
|
|
101
|
+
direction,
|
|
102
|
+
onIncomingStream: (muxedStream) => {
|
|
103
|
+
try {
|
|
104
|
+
mss.handle(muxedStream, registrar.getProtocols(), {
|
|
105
|
+
log
|
|
106
|
+
})
|
|
107
|
+
.then(async ({ stream, protocol }) => {
|
|
108
|
+
log('%s: incoming stream opened on %s', direction, protocol);
|
|
109
|
+
muxedStream.protocol = protocol;
|
|
110
|
+
muxedStream.sink = stream.sink;
|
|
111
|
+
muxedStream.source = stream.source;
|
|
112
|
+
connection.streams.push(muxedStream);
|
|
113
|
+
const { handler } = registrar.getHandler(protocol);
|
|
114
|
+
await handler({ connection, stream: muxedStream });
|
|
115
|
+
}).catch(err => {
|
|
116
|
+
log.error(err);
|
|
117
|
+
muxedStream.abort(err);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
catch (err) {
|
|
121
|
+
log.error(err);
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
onStreamEnd: (muxedStream) => {
|
|
125
|
+
connection.streams = connection.streams.filter(stream => stream.id !== muxedStream.id);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
void pipe(maConn, muxer, maConn);
|
|
129
|
+
const connection = new MockConnection({
|
|
130
|
+
remoteAddr,
|
|
131
|
+
remotePeer,
|
|
132
|
+
direction,
|
|
133
|
+
maConn,
|
|
134
|
+
muxer,
|
|
135
|
+
logger
|
|
136
|
+
});
|
|
137
|
+
return connection;
|
|
138
|
+
}
|
|
139
|
+
export function mockStream(stream, init = {}) {
|
|
140
|
+
const id = `stream-${Date.now()}`;
|
|
141
|
+
const log = logger(`libp2p:mock-stream:${id}`);
|
|
142
|
+
// ensure stream output is `Uint8ArrayList` as it would be from an actual
|
|
143
|
+
// Stream where everything is length-varint encoded
|
|
144
|
+
const originalSource = stream.source;
|
|
145
|
+
stream.source = (async function* () {
|
|
146
|
+
for await (const buf of originalSource) {
|
|
147
|
+
if (buf instanceof Uint8Array) {
|
|
148
|
+
yield new Uint8ArrayList(buf);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
yield buf;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
})();
|
|
155
|
+
const abortSinkController = new AbortController();
|
|
156
|
+
const originalSink = stream.sink.bind(stream);
|
|
157
|
+
stream.sink = async (source) => {
|
|
158
|
+
abortSinkController.signal.addEventListener('abort', () => {
|
|
159
|
+
closeSource(source, log);
|
|
160
|
+
});
|
|
161
|
+
await originalSink(source);
|
|
162
|
+
};
|
|
163
|
+
const mockStream = {
|
|
164
|
+
...stream,
|
|
165
|
+
close: async (options) => {
|
|
166
|
+
await mockStream.closeRead(options);
|
|
167
|
+
await mockStream.closeWrite(options);
|
|
168
|
+
},
|
|
169
|
+
closeRead: async () => {
|
|
170
|
+
closeSource(originalSource, log);
|
|
171
|
+
mockStream.timeline.closeRead = Date.now();
|
|
172
|
+
if (mockStream.timeline.closeWrite != null) {
|
|
173
|
+
mockStream.timeline.close = Date.now();
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
closeWrite: async () => {
|
|
177
|
+
abortSinkController.abort();
|
|
178
|
+
mockStream.timeline.closeWrite = Date.now();
|
|
179
|
+
if (mockStream.timeline.closeRead != null) {
|
|
180
|
+
mockStream.timeline.close = Date.now();
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
abort: () => {
|
|
184
|
+
closeSource(originalSource, log);
|
|
185
|
+
mockStream.timeline.closeWrite = Date.now();
|
|
186
|
+
mockStream.timeline.closeRead = Date.now();
|
|
187
|
+
mockStream.timeline.close = Date.now();
|
|
188
|
+
},
|
|
189
|
+
direction: 'outbound',
|
|
190
|
+
protocol: '/foo/1.0.0',
|
|
191
|
+
timeline: {
|
|
192
|
+
open: Date.now()
|
|
193
|
+
},
|
|
194
|
+
metadata: {},
|
|
195
|
+
id: `stream-${Date.now()}`,
|
|
196
|
+
status: 'open',
|
|
197
|
+
readStatus: 'ready',
|
|
198
|
+
writeStatus: 'ready',
|
|
199
|
+
log: logger('mock-stream'),
|
|
200
|
+
...init
|
|
201
|
+
};
|
|
202
|
+
return mockStream;
|
|
203
|
+
}
|
|
204
|
+
export function streamPair(a, b, init = {}) {
|
|
205
|
+
return [
|
|
206
|
+
mockStream(a.duplex, {
|
|
207
|
+
direction: 'outbound',
|
|
208
|
+
...init,
|
|
209
|
+
...(a.init ?? {})
|
|
210
|
+
}),
|
|
211
|
+
mockStream(b.duplex, {
|
|
212
|
+
direction: 'inbound',
|
|
213
|
+
...init,
|
|
214
|
+
...(b.init ?? {})
|
|
215
|
+
})
|
|
216
|
+
];
|
|
217
|
+
}
|
|
218
|
+
export function multiaddrConnectionPair(a, b) {
|
|
219
|
+
const [peerBtoPeerA, peerAtoPeerB] = duplexPair();
|
|
220
|
+
return [
|
|
221
|
+
mockMultiaddrConnection(peerAtoPeerB, b.peerId),
|
|
222
|
+
mockMultiaddrConnection(peerBtoPeerA, a.peerId)
|
|
223
|
+
];
|
|
224
|
+
}
|
|
225
|
+
export function connectionPair(a, b) {
|
|
226
|
+
const [peerBtoPeerA, peerAtoPeerB] = multiaddrConnectionPair(a, b);
|
|
227
|
+
return [
|
|
228
|
+
mockConnection(peerBtoPeerA, {
|
|
229
|
+
registrar: a.registrar
|
|
230
|
+
}),
|
|
231
|
+
mockConnection(peerAtoPeerB, {
|
|
232
|
+
registrar: b.registrar
|
|
233
|
+
})
|
|
234
|
+
];
|
|
235
|
+
}
|
|
236
|
+
//# sourceMappingURL=connection.js.map
|