From 230e4918874eba3c6003f4b964f2233bd6059e29 Mon Sep 17 00:00:00 2001 From: Amy Yan Date: Tue, 13 Aug 2024 13:02:24 +1000 Subject: [PATCH] wip: dependency injection relaxed types --- src/WebSocketStream.ts | 6 +++--- tests/WebSocketStream.test.ts | 17 ++++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/WebSocketStream.ts b/src/WebSocketStream.ts index 7a5672c3..8d10c3be 100644 --- a/src/WebSocketStream.ts +++ b/src/WebSocketStream.ts @@ -3,8 +3,8 @@ import type { StreamCodeToReason, StreamReasonToCode, } from './types.js'; -import type WebSocketConnection from './WebSocketConnection.js'; import type { StreamId, StreamMessage, VarInt } from './message/index.js'; +import type { Evented } from '@matrixai/events'; import type { ReadableWritablePair, WritableStreamDefaultController, @@ -102,7 +102,7 @@ class WebSocketStream implements ReadableWritablePair { public readonly writable: WritableStream; protected logger: Logger; - protected connection: WebSocketConnection; + protected connection: Evented & { meta: () => ConnectionMetadata }; protected reasonToCode: StreamReasonToCode; protected codeToReason: StreamCodeToReason; protected readableController: ReadableStreamDefaultController; @@ -199,7 +199,7 @@ class WebSocketStream implements ReadableWritablePair { }: { initiated: 'local' | 'peer'; streamId: StreamId; - connection: WebSocketConnection; + connection: Evented & { meta: () => ConnectionMetadata }; bufferSize: number; reasonToCode?: StreamReasonToCode; codeToReason?: StreamCodeToReason; diff --git a/tests/WebSocketStream.test.ts b/tests/WebSocketStream.test.ts index 548f82a8..0f5bddc6 100644 --- a/tests/WebSocketStream.test.ts +++ b/tests/WebSocketStream.test.ts @@ -28,9 +28,7 @@ const logger2 = new Logger('stream 2', LogLevel.WARN, [ let streamIdCounter = 0n; -function createMockedWebSocketConnection( - streamOptions: StreamOptions = {}, -): WebSocketConnection { +function createMockedWebSocketConnection(streamOptions: StreamOptions = {}) { const instance = new EventTarget() as EventTarget & { peerConnection: WebSocketConnection | undefined; connectTo: (connection: WebSocketConnection) => void; @@ -121,13 +119,18 @@ function createMockedWebSocketConnection( } await stream.streamRecv(remainder); }; - return instance as unknown as WebSocketConnection; + return instance; } describe(WebSocketStream.name, () => { async function createConnectionPair( streamOptions: StreamOptions = {}, - ): Promise<[WebSocketConnection, WebSocketConnection]> { + ): Promise< + [ + ReturnType, + ReturnType, + ] + > { const connection1 = createMockedWebSocketConnection(streamOptions); const connection2 = createMockedWebSocketConnection(streamOptions); (connection1 as any).connectTo(connection2); @@ -135,8 +138,8 @@ describe(WebSocketStream.name, () => { } async function createStreamPairFrom( - connection1: WebSocketConnection, - connection2: WebSocketConnection, + connection1: ReturnType, + connection2: ReturnType, ): Promise<[WebSocketStream, WebSocketStream]> { const stream1 = await connection1.newStream(); const createStream2Prom = utils.promise();