From e26848b06e77bfcff4063139c9ed816f37f05cb6 Mon Sep 17 00:00:00 2001 From: tabcat Date: Mon, 7 Aug 2023 20:47:48 +0100 Subject: [PATCH] fix: remove stream add/remove methods from connection interface (#1912) Co-authored-by: Chad Nehemiah --- packages/interface-compliance-tests/package.json | 1 - .../src/connection/index.ts | 10 +--------- .../src/mocks/connection.ts | 12 ++---------- packages/interface/src/connection/index.ts | 10 ---------- packages/libp2p/src/connection/index.ts | 14 -------------- packages/libp2p/src/upgrader.ts | 5 ----- packages/libp2p/test/connection/compliance.spec.ts | 1 - 7 files changed, 3 insertions(+), 50 deletions(-) diff --git a/packages/interface-compliance-tests/package.json b/packages/interface-compliance-tests/package.json index b788f05b8d..257d8968ab 100644 --- a/packages/interface-compliance-tests/package.json +++ b/packages/interface-compliance-tests/package.json @@ -130,7 +130,6 @@ "p-wait-for": "^5.0.2", "protons-runtime": "^5.0.0", "sinon": "^15.1.2", - "ts-sinon": "^2.0.2", "uint8arraylist": "^2.4.3", "uint8arrays": "^4.0.4" }, diff --git a/packages/interface-compliance-tests/src/connection/index.ts b/packages/interface-compliance-tests/src/connection/index.ts index 833ca30ba1..cbce6f491a 100644 --- a/packages/interface-compliance-tests/src/connection/index.ts +++ b/packages/interface-compliance-tests/src/connection/index.ts @@ -1,8 +1,7 @@ import { expect } from 'aegir/chai' import sinon from 'sinon' -import { stubInterface } from 'ts-sinon' import type { TestSetup } from '../index.js' -import type { Connection, Stream } from '@libp2p/interface/connection' +import type { Connection } from '@libp2p/interface/connection' export default (test: TestSetup): void => { describe('connection', () => { @@ -124,13 +123,6 @@ export default (test: TestSetup): void => { expect(stream).to.have.property('direction', 'outbound') }) - it.skip('should track inbound streams', async () => { - // Add an remotely opened stream - const stream = stubInterface() - connection.addStream(stream) - expect(stream).to.have.property('direction', 'inbound') - }) - it('should support a proxy on the timeline', async () => { sinon.spy(proxyHandler, 'set') expect(connection.timeline.close).to.not.exist() diff --git a/packages/interface-compliance-tests/src/mocks/connection.ts b/packages/interface-compliance-tests/src/mocks/connection.ts index 8c88ba057b..566de6d05c 100644 --- a/packages/interface-compliance-tests/src/mocks/connection.ts +++ b/packages/interface-compliance-tests/src/mocks/connection.ts @@ -94,14 +94,6 @@ class MockConnection implements Connection { return stream } - addStream (stream: Stream): void { - this.streams.push(stream) - } - - removeStream (id: string): void { - this.streams = this.streams.filter(stream => stream.id !== id) - } - async close (options?: AbortOptions): Promise { this.status = 'closing' await Promise.all( @@ -147,7 +139,7 @@ export function mockConnection (maConn: MultiaddrConnection, opts: MockConnectio muxedStream.sink = stream.sink muxedStream.source = stream.source - connection.addStream(muxedStream) + connection.streams.push(muxedStream) const { handler } = registrar.getHandler(protocol) handler({ connection, stream: muxedStream }) @@ -159,7 +151,7 @@ export function mockConnection (maConn: MultiaddrConnection, opts: MockConnectio } }, onStreamEnd: (muxedStream) => { - connection.removeStream(muxedStream.id) + connection.streams = connection.streams.filter(stream => stream.id !== muxedStream.id) } }) diff --git a/packages/interface/src/connection/index.ts b/packages/interface/src/connection/index.ts index 6a2100f75d..2bed6736a1 100644 --- a/packages/interface/src/connection/index.ts +++ b/packages/interface/src/connection/index.ts @@ -258,16 +258,6 @@ export interface Connection { */ newStream: (protocols: string | string[], options?: NewStreamOptions) => Promise - /** - * Add a stream to this connection - */ - addStream: (stream: Stream) => void - - /** - * Remove a stream from this connection - */ - removeStream: (id: string) => void - /** * Gracefully close the connection. All queued data will be written to the * underlying transport. diff --git a/packages/libp2p/src/connection/index.ts b/packages/libp2p/src/connection/index.ts index 53b1a704b3..8509b8d70a 100644 --- a/packages/libp2p/src/connection/index.ts +++ b/packages/libp2p/src/connection/index.ts @@ -138,20 +138,6 @@ export class ConnectionImpl implements Connection { return stream } - /** - * Add a stream when it is opened to the registry - */ - addStream (stream: Stream): void { - stream.direction = 'inbound' - } - - /** - * Remove stream registry after it is closed - */ - removeStream (id: string): void { - - } - /** * Close the connection */ diff --git a/packages/libp2p/src/upgrader.ts b/packages/libp2p/src/upgrader.ts index f77dcf6319..adf079b49f 100644 --- a/packages/libp2p/src/upgrader.ts +++ b/packages/libp2p/src/upgrader.ts @@ -415,7 +415,6 @@ export class DefaultUpgrader implements Upgrader { protocols: [protocol] }) - connection.addStream(muxedStream) this.components.metrics?.trackProtocolStream(muxedStream, connection) this._onStream({ connection, stream: muxedStream, protocol }) @@ -427,10 +426,6 @@ export class DefaultUpgrader implements Upgrader { await muxedStream.close() } }) - }, - // Run anytime a stream closes - onStreamEnd: muxedStream => { - connection?.removeStream(muxedStream.id) } }) diff --git a/packages/libp2p/test/connection/compliance.spec.ts b/packages/libp2p/test/connection/compliance.spec.ts index ca216acd5d..41059425b7 100644 --- a/packages/libp2p/test/connection/compliance.spec.ts +++ b/packages/libp2p/test/connection/compliance.spec.ts @@ -35,7 +35,6 @@ describe('connection compliance', () => { ...pair(), close: async () => { void stream.sink(async function * () {}()) - connection.removeStream(stream.id) openStreams = openStreams.filter(s => s.id !== id) }, closeRead: async () => {},