@libp2p/interface-compliance-tests 6.4.16-cf9aab5c8 → 6.5.0-a02cb0461
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 +24 -15
- package/dist/src/connection-encryption/index.js.map +1 -1
- package/dist/src/stream-muxer/base-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/base-test.js +62 -345
- 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 +253 -320
- 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/{mega-stress-test.d.ts → stream-test.d.ts} +2 -2
- package/dist/src/stream-muxer/stream-test.d.ts.map +1 -0
- package/dist/src/stream-muxer/stream-test.js +289 -0
- package/dist/src/stream-muxer/stream-test.js.map +1 -0
- package/dist/src/stream-muxer/stress-test.d.ts.map +1 -1
- package/dist/src/stream-muxer/stress-test.js +70 -16
- 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 +232 -203
- 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 +23 -51
- package/src/connection-encryption/index.ts +27 -20
- package/src/stream-muxer/base-test.ts +75 -413
- package/src/stream-muxer/close-test.ts +304 -343
- package/src/stream-muxer/index.ts +2 -2
- package/src/stream-muxer/stream-test.ts +380 -0
- package/src/stream-muxer/stress-test.ts +92 -18
- package/src/transport/index.ts +280 -241
- package/src/transport/utils.ts +2 -2
- package/dist/src/connection-encryption/utils/index.d.ts +0 -3
- package/dist/src/connection-encryption/utils/index.d.ts.map +0 -1
- package/dist/src/connection-encryption/utils/index.js +0 -21
- package/dist/src/connection-encryption/utils/index.js.map +0 -1
- package/dist/src/matchers.d.ts +0 -12
- package/dist/src/matchers.d.ts.map +0 -1
- package/dist/src/matchers.js +0 -14
- package/dist/src/matchers.js.map +0 -1
- package/dist/src/mocks/connection-manager.d.ts +0 -27
- package/dist/src/mocks/connection-manager.d.ts.map +0 -1
- package/dist/src/mocks/connection-manager.js +0 -147
- package/dist/src/mocks/connection-manager.js.map +0 -1
- package/dist/src/mocks/connection.d.ts +0 -41
- package/dist/src/mocks/connection.d.ts.map +0 -1
- package/dist/src/mocks/connection.js +0 -236
- package/dist/src/mocks/connection.js.map +0 -1
- package/dist/src/mocks/duplex.d.ts +0 -4
- package/dist/src/mocks/duplex.d.ts.map +0 -1
- package/dist/src/mocks/duplex.js +0 -9
- package/dist/src/mocks/duplex.js.map +0 -1
- package/dist/src/mocks/index.d.ts +0 -12
- package/dist/src/mocks/index.d.ts.map +0 -1
- package/dist/src/mocks/index.js +0 -8
- package/dist/src/mocks/index.js.map +0 -1
- package/dist/src/mocks/multiaddr-connection.d.ts +0 -17
- package/dist/src/mocks/multiaddr-connection.d.ts.map +0 -1
- package/dist/src/mocks/multiaddr-connection.js +0 -64
- package/dist/src/mocks/multiaddr-connection.js.map +0 -1
- package/dist/src/mocks/muxer.d.ts +0 -36
- package/dist/src/mocks/muxer.d.ts.map +0 -1
- package/dist/src/mocks/muxer.js +0 -234
- package/dist/src/mocks/muxer.js.map +0 -1
- package/dist/src/mocks/registrar.d.ts +0 -16
- package/dist/src/mocks/registrar.d.ts.map +0 -1
- package/dist/src/mocks/registrar.js +0 -66
- package/dist/src/mocks/registrar.js.map +0 -1
- package/dist/src/mocks/upgrader.d.ts +0 -9
- package/dist/src/mocks/upgrader.d.ts.map +0 -1
- package/dist/src/mocks/upgrader.js +0 -46
- package/dist/src/mocks/upgrader.js.map +0 -1
- package/dist/src/pubsub/api.d.ts +0 -6
- package/dist/src/pubsub/api.d.ts.map +0 -1
- package/dist/src/pubsub/api.js +0 -88
- package/dist/src/pubsub/api.js.map +0 -1
- package/dist/src/pubsub/connection-handlers.d.ts +0 -6
- package/dist/src/pubsub/connection-handlers.d.ts.map +0 -1
- package/dist/src/pubsub/connection-handlers.js +0 -329
- package/dist/src/pubsub/connection-handlers.js.map +0 -1
- package/dist/src/pubsub/emit-self.d.ts +0 -6
- package/dist/src/pubsub/emit-self.d.ts.map +0 -1
- package/dist/src/pubsub/emit-self.js +0 -80
- package/dist/src/pubsub/emit-self.js.map +0 -1
- package/dist/src/pubsub/index.d.ts +0 -18
- package/dist/src/pubsub/index.d.ts.map +0 -1
- package/dist/src/pubsub/index.js +0 -17
- package/dist/src/pubsub/index.js.map +0 -1
- package/dist/src/pubsub/messages.d.ts +0 -6
- package/dist/src/pubsub/messages.d.ts.map +0 -1
- package/dist/src/pubsub/messages.js +0 -48
- package/dist/src/pubsub/messages.js.map +0 -1
- package/dist/src/pubsub/multiple-nodes.d.ts +0 -6
- package/dist/src/pubsub/multiple-nodes.d.ts.map +0 -1
- package/dist/src/pubsub/multiple-nodes.js +0 -350
- package/dist/src/pubsub/multiple-nodes.js.map +0 -1
- package/dist/src/pubsub/two-nodes.d.ts +0 -6
- package/dist/src/pubsub/two-nodes.d.ts.map +0 -1
- package/dist/src/pubsub/two-nodes.js +0 -216
- package/dist/src/pubsub/two-nodes.js.map +0 -1
- package/dist/src/pubsub/utils.d.ts +0 -5
- package/dist/src/pubsub/utils.d.ts.map +0 -1
- package/dist/src/pubsub/utils.js +0 -27
- package/dist/src/pubsub/utils.js.map +0 -1
- package/dist/src/stream-muxer/mega-stress-test.d.ts.map +0 -1
- package/dist/src/stream-muxer/mega-stress-test.js +0 -11
- package/dist/src/stream-muxer/mega-stress-test.js.map +0 -1
- package/dist/src/stream-muxer/spawner.d.ts +0 -4
- package/dist/src/stream-muxer/spawner.d.ts.map +0 -1
- package/dist/src/stream-muxer/spawner.js +0 -39
- package/dist/src/stream-muxer/spawner.js.map +0 -1
- package/src/connection-encryption/utils/index.ts +0 -27
- package/src/matchers.ts +0 -18
- package/src/mocks/connection-manager.ts +0 -216
- package/src/mocks/connection.ts +0 -309
- package/src/mocks/duplex.ts +0 -11
- package/src/mocks/index.ts +0 -11
- package/src/mocks/multiaddr-connection.ts +0 -80
- package/src/mocks/muxer.ts +0 -331
- package/src/mocks/registrar.ts +0 -86
- package/src/mocks/upgrader.ts +0 -65
- package/src/pubsub/api.ts +0 -116
- package/src/pubsub/connection-handlers.ts +0 -413
- package/src/pubsub/emit-self.ts +0 -99
- package/src/pubsub/index.ts +0 -34
- package/src/pubsub/messages.ts +0 -59
- package/src/pubsub/multiple-nodes.ts +0 -440
- package/src/pubsub/two-nodes.ts +0 -272
- package/src/pubsub/utils.ts +0 -34
- package/src/stream-muxer/mega-stress-test.ts +0 -14
- package/src/stream-muxer/spawner.ts +0 -57
package/dist/src/pubsub/utils.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { generateKeyPair } from '@libp2p/crypto/keys';
|
|
2
|
-
import { defaultLogger } from '@libp2p/logger';
|
|
3
|
-
import { peerIdFromPrivateKey } from '@libp2p/peer-id';
|
|
4
|
-
import { TypedEventEmitter } from 'main-event';
|
|
5
|
-
import { pEvent } from 'p-event';
|
|
6
|
-
import pWaitFor from 'p-wait-for';
|
|
7
|
-
import { mockConnectionManager, mockRegistrar, mockNetwork } from '../mocks/index.js';
|
|
8
|
-
export async function waitForSubscriptionUpdate(a, b) {
|
|
9
|
-
await pWaitFor(async () => {
|
|
10
|
-
const event = await pEvent(a, 'subscription-change');
|
|
11
|
-
return event.detail.peerId.equals(b);
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
export async function createComponents() {
|
|
15
|
-
const privateKey = await generateKeyPair('Ed25519');
|
|
16
|
-
const components = {
|
|
17
|
-
peerId: peerIdFromPrivateKey(privateKey),
|
|
18
|
-
privateKey,
|
|
19
|
-
registrar: mockRegistrar(),
|
|
20
|
-
events: new TypedEventEmitter(),
|
|
21
|
-
logger: defaultLogger()
|
|
22
|
-
};
|
|
23
|
-
components.connectionManager = mockConnectionManager(components);
|
|
24
|
-
mockNetwork.addNode(components);
|
|
25
|
-
return components;
|
|
26
|
-
}
|
|
27
|
-
//# sourceMappingURL=utils.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/pubsub/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAA;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAChC,OAAO,QAAQ,MAAM,YAAY,CAAA;AACjC,OAAO,EAAE,qBAAqB,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAIrF,MAAM,CAAC,KAAK,UAAU,yBAAyB,CAAE,CAAS,EAAE,CAAS;IACnE,MAAM,QAAQ,CAAC,KAAK,IAAI,EAAE;QACxB,MAAM,KAAK,GAAG,MAAM,MAAM,CAA6D,CAAC,EAAE,qBAAqB,CAAC,CAAA;QAEhH,OAAO,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;IACtC,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,gBAAgB;IACpC,MAAM,UAAU,GAAG,MAAM,eAAe,CAAC,SAAS,CAAC,CAAA;IAEnD,MAAM,UAAU,GAAQ;QACtB,MAAM,EAAE,oBAAoB,CAAC,UAAU,CAAC;QACxC,UAAU;QACV,SAAS,EAAE,aAAa,EAAE;QAC1B,MAAM,EAAE,IAAI,iBAAiB,EAAE;QAC/B,MAAM,EAAE,aAAa,EAAE;KACxB,CAAA;IACD,UAAU,CAAC,iBAAiB,GAAG,qBAAqB,CAAC,UAAU,CAAC,CAAA;IAEhE,WAAW,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;IAE/B,OAAO,UAAU,CAAA;AACnB,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mega-stress-test.d.ts","sourceRoot":"","sources":["../../../src/stream-muxer/mega-stress-test.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,KAAK,EAAe,kBAAkB,EAAmB,MAAM,mBAAmB,CAAA;yBAEzE,QAAQ,SAAS,CAAC,kBAAkB,CAAC,KAAG,IAAI;AAA5D,wBASC"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import spawn from './spawner.js';
|
|
2
|
-
export default (common) => {
|
|
3
|
-
const createMuxer = async (init) => {
|
|
4
|
-
const factory = await common.setup();
|
|
5
|
-
return factory.createStreamMuxer(init);
|
|
6
|
-
};
|
|
7
|
-
describe.skip('mega stress test', function () {
|
|
8
|
-
it('10,000 streams with 10,000 msg', async () => { await spawn(createMuxer, 10000, 10000, 5000); });
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
//# sourceMappingURL=mega-stress-test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mega-stress-test.js","sourceRoot":"","sources":["../../../src/stream-muxer/mega-stress-test.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,cAAc,CAAA;AAIhC,eAAe,CAAC,MAAqC,EAAQ,EAAE;IAC7D,MAAM,WAAW,GAAG,KAAK,EAAE,IAAsB,EAAwB,EAAE;QACzE,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,KAAK,EAAE,CAAA;QACpC,OAAO,OAAO,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAA;IACxC,CAAC,CAAA;IAED,QAAQ,CAAC,IAAI,CAAC,kBAAkB,EAAE;QAChC,EAAE,CAAC,gCAAgC,EAAE,KAAK,IAAI,EAAE,GAAG,MAAM,KAAK,CAAC,WAAW,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,CAAA,CAAC,CAAC,CAAC,CAAA;IACpG,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { StreamMuxer, StreamMuxerInit } from '@libp2p/interface';
|
|
2
|
-
declare const _default: (createMuxer: (init?: StreamMuxerInit) => Promise<StreamMuxer>, nStreams: number, nMsg: number, limit?: number) => Promise<void>;
|
|
3
|
-
export default _default;
|
|
4
|
-
//# sourceMappingURL=spawner.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"spawner.d.ts","sourceRoot":"","sources":["../../../src/stream-muxer/spawner.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;yBAE/C,aAAa,CAAC,IAAI,CAAC,EAAE,eAAe,KAAK,OAAO,CAAC,WAAW,CAAC,EAAE,UAAU,MAAM,EAAE,MAAM,MAAM,EAAE,QAAQ,MAAM,KAAG,OAAO,CAAC,IAAI,CAAC;AAAnJ,wBA8CC"}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { expect } from 'aegir/chai';
|
|
2
|
-
import all from 'it-all';
|
|
3
|
-
import drain from 'it-drain';
|
|
4
|
-
import { duplexPair } from 'it-pair/duplex';
|
|
5
|
-
import { pipe } from 'it-pipe';
|
|
6
|
-
import pLimit from 'p-limit';
|
|
7
|
-
import { Uint8ArrayList } from 'uint8arraylist';
|
|
8
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
9
|
-
export default async (createMuxer, nStreams, nMsg, limit) => {
|
|
10
|
-
const [dialerSocket, listenerSocket] = duplexPair();
|
|
11
|
-
const msg = new Uint8ArrayList(uint8ArrayFromString('simple msg'));
|
|
12
|
-
const listener = await createMuxer({
|
|
13
|
-
direction: 'inbound',
|
|
14
|
-
onIncomingStream: (stream) => {
|
|
15
|
-
void pipe(stream, drain).then(async () => {
|
|
16
|
-
await stream.close();
|
|
17
|
-
})
|
|
18
|
-
.catch(err => { stream.abort(err); });
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
const dialer = await createMuxer({
|
|
22
|
-
direction: 'outbound'
|
|
23
|
-
});
|
|
24
|
-
void pipe(listenerSocket, listener, listenerSocket);
|
|
25
|
-
void pipe(dialerSocket, dialer, dialerSocket);
|
|
26
|
-
const spawnStream = async () => {
|
|
27
|
-
const stream = await dialer.newStream();
|
|
28
|
-
expect(stream).to.exist; // eslint-disable-line
|
|
29
|
-
const res = await pipe((async function* () {
|
|
30
|
-
for (let i = 0; i < nMsg; i++) {
|
|
31
|
-
yield msg;
|
|
32
|
-
}
|
|
33
|
-
}()), stream, async (source) => all(source));
|
|
34
|
-
expect(res).to.be.eql([]);
|
|
35
|
-
};
|
|
36
|
-
const limiter = pLimit(limit ?? Infinity);
|
|
37
|
-
await Promise.all(Array.from(Array(nStreams), async () => { await limiter(async () => { await spawnStream(); }); }));
|
|
38
|
-
};
|
|
39
|
-
//# sourceMappingURL=spawner.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"spawner.js","sourceRoot":"","sources":["../../../src/stream-muxer/spawner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACnC,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,KAAK,MAAM,UAAU,CAAA;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,MAAM,MAAM,SAAS,CAAA;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAG5E,eAAe,KAAK,EAAE,WAA6D,EAAE,QAAgB,EAAE,IAAY,EAAE,KAAc,EAAiB,EAAE;IACpJ,MAAM,CAAC,YAAY,EAAE,cAAc,CAAC,GAAG,UAAU,EAA+B,CAAA;IAEhF,MAAM,GAAG,GAAG,IAAI,cAAc,CAAC,oBAAoB,CAAC,YAAY,CAAC,CAAC,CAAA;IAElE,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC;QACjC,SAAS,EAAE,SAAS;QACpB,gBAAgB,EAAE,CAAC,MAAM,EAAE,EAAE;YAC3B,KAAK,IAAI,CACP,MAAM,EACN,KAAK,CACN,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE;gBAChB,MAAM,MAAM,CAAC,KAAK,EAAE,CAAA;YACtB,CAAC,CAAC;iBACC,KAAK,CAAC,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA,CAAC,CAAC,CAAC,CAAA;QACxC,CAAC;KACF,CAAC,CAAA;IACF,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC;QAC/B,SAAS,EAAE,UAAU;KACtB,CAAC,CAAA;IAEF,KAAK,IAAI,CAAC,cAAc,EAAE,QAAQ,EAAE,cAAc,CAAC,CAAA;IACnD,KAAK,IAAI,CAAC,YAAY,EAAE,MAAM,EAAE,YAAY,CAAC,CAAA;IAE7C,MAAM,WAAW,GAAG,KAAK,IAAmB,EAAE;QAC5C,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,SAAS,EAAE,CAAA;QACvC,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,CAAA,CAAC,sBAAsB;QAE9C,MAAM,GAAG,GAAG,MAAM,IAAI,CACpB,CAAC,KAAK,SAAU,CAAC;YACf,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;gBAC9B,MAAM,GAAG,CAAA;YACX,CAAC;QACH,CAAC,EAAE,CAAC,EACJ,MAAM,EACN,KAAK,EAAE,MAAM,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAC9B,CAAA;QAED,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;IAC3B,CAAC,CAAA;IAED,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,IAAI,QAAQ,CAAC,CAAA;IAEzC,MAAM,OAAO,CAAC,GAAG,CACf,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,KAAK,IAAI,EAAE,GAAG,MAAM,OAAO,CAAC,KAAK,IAAI,EAAE,GAAG,MAAM,WAAW,EAAE,CAAA,CAAC,CAAC,CAAC,CAAA,CAAC,CAAC,CAAC,CAChG,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { logger } from '@libp2p/logger'
|
|
2
|
-
import { multiaddr } from '@multiformats/multiaddr'
|
|
3
|
-
import { duplexPair } from 'it-pair/duplex'
|
|
4
|
-
import type { MultiaddrConnection } from '@libp2p/interface'
|
|
5
|
-
import type { Duplex, Source } from 'it-stream-types'
|
|
6
|
-
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
7
|
-
|
|
8
|
-
export function createMaConnPair (): [MultiaddrConnection, MultiaddrConnection] {
|
|
9
|
-
const [local, remote] = duplexPair<Uint8Array | Uint8ArrayList>()
|
|
10
|
-
|
|
11
|
-
function duplexToMaConn (duplex: Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array | Uint8ArrayList>, Promise<void>>): MultiaddrConnection {
|
|
12
|
-
const output: MultiaddrConnection = {
|
|
13
|
-
...duplex,
|
|
14
|
-
close: async () => {},
|
|
15
|
-
abort: () => {},
|
|
16
|
-
remoteAddr: multiaddr('/ip4/127.0.0.1/tcp/4001'),
|
|
17
|
-
timeline: {
|
|
18
|
-
open: Date.now()
|
|
19
|
-
},
|
|
20
|
-
log: logger('duplex-maconn')
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return output
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return [duplexToMaConn(local), duplexToMaConn(remote)]
|
|
27
|
-
}
|
package/src/matchers.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import Sinon from 'sinon'
|
|
2
|
-
import type { PeerId } from '@libp2p/interface'
|
|
3
|
-
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
4
|
-
import type { SinonMatcher } from 'sinon'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @deprecated PeerIds can be passed to sinon matchers directly
|
|
8
|
-
*/
|
|
9
|
-
export function matchPeerId (peerId: PeerId): SinonMatcher {
|
|
10
|
-
return Sinon.match(p => p.toString() === peerId.toString())
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @deprecated Multiaddrs can be passed to sinon matchers directly
|
|
15
|
-
*/
|
|
16
|
-
export function matchMultiaddr (ma: Multiaddr): SinonMatcher {
|
|
17
|
-
return Sinon.match(m => m.toString() === ma.toString())
|
|
18
|
-
}
|
|
@@ -1,216 +0,0 @@
|
|
|
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
|
-
import type { PrivateKey, PeerId, ComponentLogger, Libp2pEvents, PendingDial, Connection, PubSub, Startable } from '@libp2p/interface'
|
|
7
|
-
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
|
|
8
|
-
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
9
|
-
import type { AbortOptions } from 'it-pushable'
|
|
10
|
-
import type { TypedEventTarget } from 'main-event'
|
|
11
|
-
|
|
12
|
-
export interface MockNetworkComponents {
|
|
13
|
-
peerId: PeerId
|
|
14
|
-
privateKey: PrivateKey
|
|
15
|
-
registrar: Registrar
|
|
16
|
-
connectionManager: ConnectionManager
|
|
17
|
-
events: TypedEventTarget<Libp2pEvents>
|
|
18
|
-
pubsub?: PubSub
|
|
19
|
-
logger: ComponentLogger
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export class MockNetwork {
|
|
23
|
-
private components: MockNetworkComponents[] = []
|
|
24
|
-
|
|
25
|
-
addNode (components: MockNetworkComponents): void {
|
|
26
|
-
this.components.push(components)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
getNode (peerId: PeerId | Multiaddr []): MockNetworkComponents {
|
|
30
|
-
if (Array.isArray(peerId) && peerId.length > 0) {
|
|
31
|
-
peerId = peerIdFromString(peerId[0].getPeerId() ?? '')
|
|
32
|
-
} else if (isPeerId(peerId)) {
|
|
33
|
-
for (const components of this.components) {
|
|
34
|
-
if (peerId.equals(components.peerId)) {
|
|
35
|
-
return components
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
throw new Error('Peer not found')
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
reset (): void {
|
|
44
|
-
this.components = []
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export const mockNetwork = new MockNetwork()
|
|
49
|
-
|
|
50
|
-
export interface MockConnectionManagerComponents {
|
|
51
|
-
peerId: PeerId
|
|
52
|
-
registrar: Registrar
|
|
53
|
-
events: TypedEventTarget<Libp2pEvents>
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
class MockConnectionManager implements ConnectionManager, Startable {
|
|
57
|
-
private connections: Connection[] = []
|
|
58
|
-
private readonly components: MockConnectionManagerComponents
|
|
59
|
-
private started = false
|
|
60
|
-
|
|
61
|
-
constructor (components: MockConnectionManagerComponents) {
|
|
62
|
-
this.components = components
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
isStarted (): boolean {
|
|
66
|
-
return this.started
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async start (): Promise<void> {
|
|
70
|
-
this.started = true
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async stop (): Promise<void> {
|
|
74
|
-
for (const connection of this.connections) {
|
|
75
|
-
await this.closeConnections(connection.remotePeer)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
this.started = false
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
getConnections (peerId?: PeerId): Connection[] {
|
|
82
|
-
if (peerId != null) {
|
|
83
|
-
return this.connections
|
|
84
|
-
.filter(c => c.remotePeer.toString() === peerId.toString())
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return this.connections
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
getConnectionsMap (): PeerMap<Connection[]> {
|
|
91
|
-
const map = new PeerMap<Connection[]>()
|
|
92
|
-
|
|
93
|
-
for (const conn of this.connections) {
|
|
94
|
-
const conns: Connection[] = map.get(conn.remotePeer) ?? []
|
|
95
|
-
conns.push(conn)
|
|
96
|
-
|
|
97
|
-
map.set(conn.remotePeer, conns)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return map
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
getMaxConnections (): number {
|
|
104
|
-
return 10_000
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
setMaxConnections (): void {
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
async openConnection (peerId: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<Connection> {
|
|
112
|
-
if (isMultiaddr(peerId)) {
|
|
113
|
-
throw new UnsupportedOperationError('Dialing multiaddrs not supported')
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
options?.signal?.throwIfAborted()
|
|
117
|
-
|
|
118
|
-
let existingConnections: Connection[] = []
|
|
119
|
-
|
|
120
|
-
if (Array.isArray(peerId) && peerId.length > 0) {
|
|
121
|
-
existingConnections = this.getConnections(peerIdFromString(peerId[0].getPeerId() ?? ''))
|
|
122
|
-
} else if (isPeerId(peerId)) {
|
|
123
|
-
existingConnections = this.getConnections(peerId)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (existingConnections.length > 0) {
|
|
127
|
-
return existingConnections[0]
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const componentsB = mockNetwork.getNode(peerId)
|
|
131
|
-
|
|
132
|
-
const [aToB, bToA] = connectionPair(this.components, componentsB)
|
|
133
|
-
|
|
134
|
-
// track connections
|
|
135
|
-
this.connections.push(aToB)
|
|
136
|
-
;(componentsB.connectionManager as MockConnectionManager).connections.push(bToA)
|
|
137
|
-
|
|
138
|
-
this.components.events.safeDispatchEvent('connection:open', {
|
|
139
|
-
detail: aToB
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
for (const protocol of this.components.registrar.getProtocols()) {
|
|
143
|
-
for (const topology of this.components.registrar.getTopologies(protocol)) {
|
|
144
|
-
topology.onConnect?.(componentsB.peerId, aToB)
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
this.components.events.safeDispatchEvent('peer:connect', { detail: componentsB.peerId })
|
|
149
|
-
|
|
150
|
-
componentsB.events.safeDispatchEvent('connection:open', {
|
|
151
|
-
detail: bToA
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
for (const protocol of componentsB.registrar.getProtocols()) {
|
|
155
|
-
for (const topology of componentsB.registrar.getTopologies(protocol)) {
|
|
156
|
-
topology.onConnect?.(this.components.peerId, bToA)
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
componentsB.events.safeDispatchEvent('peer:connect', { detail: this.components.peerId })
|
|
161
|
-
|
|
162
|
-
return aToB
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
async closeConnections (peerId: PeerId): Promise<void> {
|
|
166
|
-
const connections = this.getConnections(peerId)
|
|
167
|
-
|
|
168
|
-
if (connections.length === 0) {
|
|
169
|
-
return
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const componentsB = mockNetwork.getNode(peerId)
|
|
173
|
-
|
|
174
|
-
for (const protocol of this.components.registrar.getProtocols()) {
|
|
175
|
-
this.components.registrar.getTopologies(protocol).forEach(topology => {
|
|
176
|
-
topology.onDisconnect?.(componentsB.peerId)
|
|
177
|
-
})
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
for (const conn of connections) {
|
|
181
|
-
await conn.close()
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
this.connections = this.connections.filter(c => !c.remotePeer.equals(peerId))
|
|
185
|
-
|
|
186
|
-
if (this.connections.filter(c => !c.remotePeer.equals(peerId)).length === 0) {
|
|
187
|
-
componentsB.events.safeDispatchEvent('peer:disconnect', { detail: peerId })
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
await componentsB.connectionManager?.closeConnections(this.components.peerId)
|
|
191
|
-
|
|
192
|
-
if (componentsB.connectionManager?.getConnectionsMap().get(this.components.peerId) == null) {
|
|
193
|
-
componentsB.events.safeDispatchEvent('peer:disconnect', { detail: this.components.peerId })
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
async acceptIncomingConnection (): Promise<boolean> {
|
|
198
|
-
return true
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
afterUpgradeInbound (): void {
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
getDialQueue (): PendingDial[] {
|
|
206
|
-
return []
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
async isDialable (): Promise<boolean> {
|
|
210
|
-
return true
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
export function mockConnectionManager (components: MockConnectionManagerComponents): ConnectionManager {
|
|
215
|
-
return new MockConnectionManager(components)
|
|
216
|
-
}
|
package/src/mocks/connection.ts
DELETED
|
@@ -1,309 +0,0 @@
|
|
|
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
|
-
import type { AbortOptions, ComponentLogger, Logger, MultiaddrConnection, Connection, Stream, Direction, ConnectionTimeline, ConnectionStatus, PeerId, StreamMuxer, StreamMuxerFactory, NewStreamOptions, ConnectionLimits } from '@libp2p/interface'
|
|
13
|
-
import type { Registrar } from '@libp2p/interface-internal'
|
|
14
|
-
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
15
|
-
import type { Duplex, Source } from 'it-stream-types'
|
|
16
|
-
|
|
17
|
-
export interface MockConnectionOptions {
|
|
18
|
-
direction?: Direction
|
|
19
|
-
registrar?: Registrar
|
|
20
|
-
muxerFactory?: StreamMuxerFactory
|
|
21
|
-
logger?: ComponentLogger
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
interface MockConnectionInit {
|
|
25
|
-
remoteAddr: Multiaddr
|
|
26
|
-
remotePeer: PeerId
|
|
27
|
-
direction: Direction
|
|
28
|
-
maConn: MultiaddrConnection
|
|
29
|
-
muxer: StreamMuxer
|
|
30
|
-
logger: ComponentLogger
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
class MockConnection implements Connection {
|
|
34
|
-
public id: string
|
|
35
|
-
public remoteAddr: Multiaddr
|
|
36
|
-
public remotePeer: PeerId
|
|
37
|
-
public direction: Direction
|
|
38
|
-
public timeline: ConnectionTimeline
|
|
39
|
-
public multiplexer?: string
|
|
40
|
-
public encryption?: string
|
|
41
|
-
public status: ConnectionStatus
|
|
42
|
-
public streams: Stream[]
|
|
43
|
-
public tags: string[]
|
|
44
|
-
public limits?: ConnectionLimits
|
|
45
|
-
public log: Logger
|
|
46
|
-
|
|
47
|
-
private readonly muxer: StreamMuxer
|
|
48
|
-
private readonly maConn: MultiaddrConnection
|
|
49
|
-
private readonly logger: ComponentLogger
|
|
50
|
-
|
|
51
|
-
constructor (init: MockConnectionInit) {
|
|
52
|
-
const { remoteAddr, remotePeer, direction, maConn, muxer, logger } = init
|
|
53
|
-
|
|
54
|
-
this.id = `mock-connection-${Math.random()}`
|
|
55
|
-
this.remoteAddr = remoteAddr
|
|
56
|
-
this.remotePeer = remotePeer
|
|
57
|
-
this.direction = direction
|
|
58
|
-
this.status = 'open'
|
|
59
|
-
this.direction = direction
|
|
60
|
-
this.timeline = maConn.timeline
|
|
61
|
-
this.multiplexer = 'test-multiplexer'
|
|
62
|
-
this.encryption = 'yes-yes-very-secure'
|
|
63
|
-
this.streams = []
|
|
64
|
-
this.tags = []
|
|
65
|
-
this.muxer = muxer
|
|
66
|
-
this.maConn = maConn
|
|
67
|
-
this.logger = logger
|
|
68
|
-
this.log = logger.forComponent(this.id)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async newStream (protocols: string | string[], options?: NewStreamOptions): Promise<Stream> {
|
|
72
|
-
if (!Array.isArray(protocols)) {
|
|
73
|
-
protocols = [protocols]
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (protocols.length === 0) {
|
|
77
|
-
throw new Error('protocols must have a length')
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (this.status !== 'open') {
|
|
81
|
-
throw new ConnectionClosedError('connection must be open to create streams')
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
options?.signal?.throwIfAborted()
|
|
85
|
-
|
|
86
|
-
const id = `${Math.random()}`
|
|
87
|
-
const stream = await this.muxer.newStream(id)
|
|
88
|
-
const result = await mss.select(stream, protocols, {
|
|
89
|
-
...options,
|
|
90
|
-
log: this.logger.forComponent('libp2p:mock-connection:stream:mss:select')
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
stream.protocol = result.protocol
|
|
94
|
-
stream.direction = 'outbound'
|
|
95
|
-
stream.sink = result.stream.sink
|
|
96
|
-
stream.source = result.stream.source
|
|
97
|
-
|
|
98
|
-
this.streams.push(stream)
|
|
99
|
-
|
|
100
|
-
return stream
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async close (options?: AbortOptions): Promise<void> {
|
|
104
|
-
this.status = 'closing'
|
|
105
|
-
await Promise.all(
|
|
106
|
-
this.streams.map(async s => s.close(options))
|
|
107
|
-
)
|
|
108
|
-
await this.maConn.close()
|
|
109
|
-
this.status = 'closed'
|
|
110
|
-
this.timeline.close = Date.now()
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
abort (err: Error): void {
|
|
114
|
-
this.status = 'closing'
|
|
115
|
-
this.streams.forEach(s => {
|
|
116
|
-
s.abort(err)
|
|
117
|
-
})
|
|
118
|
-
this.maConn.abort(err)
|
|
119
|
-
this.status = 'closed'
|
|
120
|
-
this.timeline.close = Date.now()
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function mockConnection (maConn: MultiaddrConnection, opts: MockConnectionOptions = {}): Connection {
|
|
125
|
-
const remoteAddr = maConn.remoteAddr
|
|
126
|
-
const remotePeerIdStr = remoteAddr.getPeerId() ?? '12D3KooWCrhmFM1BCPGBkNzbPfDk4cjYmtAYSpZwUBC69Qg2kZyq'
|
|
127
|
-
const logger = opts.logger ?? defaultLogger()
|
|
128
|
-
|
|
129
|
-
if (remotePeerIdStr == null) {
|
|
130
|
-
throw new Error('Remote multiaddr must contain a peer id')
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const remotePeer = peerIdFromString(remotePeerIdStr)
|
|
134
|
-
const direction = opts.direction ?? 'inbound'
|
|
135
|
-
const registrar = opts.registrar ?? mockRegistrar()
|
|
136
|
-
const muxerFactory = opts.muxerFactory ?? mockMuxer()
|
|
137
|
-
const log = logger.forComponent('libp2p:mock-muxer')
|
|
138
|
-
|
|
139
|
-
const muxer = muxerFactory.createStreamMuxer({
|
|
140
|
-
log,
|
|
141
|
-
direction,
|
|
142
|
-
onIncomingStream: (muxedStream) => {
|
|
143
|
-
try {
|
|
144
|
-
mss.handle(muxedStream, registrar.getProtocols(), {
|
|
145
|
-
log
|
|
146
|
-
})
|
|
147
|
-
.then(async ({ stream, protocol }) => {
|
|
148
|
-
log('%s: incoming stream opened on %s', direction, protocol)
|
|
149
|
-
muxedStream.protocol = protocol
|
|
150
|
-
muxedStream.sink = stream.sink
|
|
151
|
-
muxedStream.source = stream.source
|
|
152
|
-
|
|
153
|
-
connection.streams.push(muxedStream)
|
|
154
|
-
const { handler } = registrar.getHandler(protocol)
|
|
155
|
-
|
|
156
|
-
await handler({ connection, stream: muxedStream })
|
|
157
|
-
}).catch(err => {
|
|
158
|
-
log.error(err)
|
|
159
|
-
muxedStream.abort(err)
|
|
160
|
-
})
|
|
161
|
-
} catch (err: any) {
|
|
162
|
-
log.error(err)
|
|
163
|
-
}
|
|
164
|
-
},
|
|
165
|
-
onStreamEnd: (muxedStream) => {
|
|
166
|
-
connection.streams = connection.streams.filter(stream => stream.id !== muxedStream.id)
|
|
167
|
-
}
|
|
168
|
-
})
|
|
169
|
-
|
|
170
|
-
void pipe(
|
|
171
|
-
maConn, muxer, maConn
|
|
172
|
-
)
|
|
173
|
-
|
|
174
|
-
const connection = new MockConnection({
|
|
175
|
-
remoteAddr,
|
|
176
|
-
remotePeer,
|
|
177
|
-
direction,
|
|
178
|
-
maConn,
|
|
179
|
-
muxer,
|
|
180
|
-
logger
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
return connection
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
export interface StreamInit {
|
|
187
|
-
direction?: Direction
|
|
188
|
-
protocol?: string
|
|
189
|
-
id?: string
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
export function mockStream (stream: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>, init: StreamInit = {}): Stream {
|
|
193
|
-
const id = `stream-${Date.now()}`
|
|
194
|
-
const log = logger(`libp2p:mock-stream:${id}`)
|
|
195
|
-
|
|
196
|
-
// ensure stream output is `Uint8ArrayList` as it would be from an actual
|
|
197
|
-
// Stream where everything is length-varint encoded
|
|
198
|
-
const originalSource = stream.source
|
|
199
|
-
stream.source = (async function * (): AsyncGenerator<Uint8ArrayList, any, unknown> {
|
|
200
|
-
for await (const buf of originalSource) {
|
|
201
|
-
if (buf instanceof Uint8Array) {
|
|
202
|
-
yield new Uint8ArrayList(buf)
|
|
203
|
-
} else {
|
|
204
|
-
yield buf
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
})()
|
|
208
|
-
|
|
209
|
-
const abortSinkController = new AbortController()
|
|
210
|
-
const originalSink = stream.sink.bind(stream)
|
|
211
|
-
stream.sink = async (source) => {
|
|
212
|
-
abortSinkController.signal.addEventListener('abort', () => {
|
|
213
|
-
closeSource(source, log)
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
await originalSink(source)
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const mockStream: Stream = {
|
|
220
|
-
...stream,
|
|
221
|
-
close: async (options) => {
|
|
222
|
-
await mockStream.closeRead(options)
|
|
223
|
-
await mockStream.closeWrite(options)
|
|
224
|
-
},
|
|
225
|
-
closeRead: async () => {
|
|
226
|
-
closeSource(originalSource, log)
|
|
227
|
-
mockStream.timeline.closeRead = Date.now()
|
|
228
|
-
|
|
229
|
-
if (mockStream.timeline.closeWrite != null) {
|
|
230
|
-
mockStream.timeline.close = Date.now()
|
|
231
|
-
}
|
|
232
|
-
},
|
|
233
|
-
closeWrite: async () => {
|
|
234
|
-
abortSinkController.abort()
|
|
235
|
-
mockStream.timeline.closeWrite = Date.now()
|
|
236
|
-
|
|
237
|
-
if (mockStream.timeline.closeRead != null) {
|
|
238
|
-
mockStream.timeline.close = Date.now()
|
|
239
|
-
}
|
|
240
|
-
},
|
|
241
|
-
abort: () => {
|
|
242
|
-
closeSource(originalSource, log)
|
|
243
|
-
mockStream.timeline.closeWrite = Date.now()
|
|
244
|
-
mockStream.timeline.closeRead = Date.now()
|
|
245
|
-
mockStream.timeline.close = Date.now()
|
|
246
|
-
},
|
|
247
|
-
direction: 'outbound',
|
|
248
|
-
protocol: '/foo/1.0.0',
|
|
249
|
-
timeline: {
|
|
250
|
-
open: Date.now()
|
|
251
|
-
},
|
|
252
|
-
metadata: {},
|
|
253
|
-
id: `stream-${Date.now()}`,
|
|
254
|
-
status: 'open',
|
|
255
|
-
readStatus: 'ready',
|
|
256
|
-
writeStatus: 'ready',
|
|
257
|
-
log: logger('mock-stream'),
|
|
258
|
-
...init
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
return mockStream
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
export interface StreamPairInit {
|
|
265
|
-
duplex: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>>
|
|
266
|
-
init?: StreamInit
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export function streamPair (a: StreamPairInit, b: StreamPairInit, init: StreamInit = {}): [Stream, Stream] {
|
|
270
|
-
return [
|
|
271
|
-
mockStream(a.duplex, {
|
|
272
|
-
direction: 'outbound',
|
|
273
|
-
...init,
|
|
274
|
-
...(a.init ?? {})
|
|
275
|
-
}),
|
|
276
|
-
mockStream(b.duplex, {
|
|
277
|
-
direction: 'inbound',
|
|
278
|
-
...init,
|
|
279
|
-
...(b.init ?? {})
|
|
280
|
-
})
|
|
281
|
-
]
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
export interface Peer {
|
|
285
|
-
peerId: PeerId
|
|
286
|
-
registrar: Registrar
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
export function multiaddrConnectionPair (a: { peerId: PeerId, registrar: Registrar }, b: { peerId: PeerId, registrar: Registrar }): [ MultiaddrConnection, MultiaddrConnection ] {
|
|
290
|
-
const [peerBtoPeerA, peerAtoPeerB] = duplexPair<Uint8Array | Uint8ArrayList>()
|
|
291
|
-
|
|
292
|
-
return [
|
|
293
|
-
mockMultiaddrConnection(peerAtoPeerB, b.peerId),
|
|
294
|
-
mockMultiaddrConnection(peerBtoPeerA, a.peerId)
|
|
295
|
-
]
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
export function connectionPair (a: { peerId: PeerId, registrar: Registrar }, b: { peerId: PeerId, registrar: Registrar }): [ Connection, Connection ] {
|
|
299
|
-
const [peerBtoPeerA, peerAtoPeerB] = multiaddrConnectionPair(a, b)
|
|
300
|
-
|
|
301
|
-
return [
|
|
302
|
-
mockConnection(peerBtoPeerA, {
|
|
303
|
-
registrar: a.registrar
|
|
304
|
-
}),
|
|
305
|
-
mockConnection(peerAtoPeerB, {
|
|
306
|
-
registrar: b.registrar
|
|
307
|
-
})
|
|
308
|
-
]
|
|
309
|
-
}
|
package/src/mocks/duplex.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { Duplex, Source } from 'it-stream-types'
|
|
2
|
-
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
3
|
-
|
|
4
|
-
export function mockDuplex (): Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array | Uint8ArrayList>, Promise<void>> {
|
|
5
|
-
return {
|
|
6
|
-
source: (async function * () {
|
|
7
|
-
yield * []
|
|
8
|
-
}()),
|
|
9
|
-
sink: async () => {}
|
|
10
|
-
}
|
|
11
|
-
}
|