From 90c061477328a757b49e465e4b9ca53672bb346f Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 1 Sep 2023 20:55:08 -0400 Subject: [PATCH] refactor: add end method to transports --- src/transports/base.ts | 2 ++ src/transports/tcp.ts | 5 +++++ src/transports/webSocket.ts | 3 +++ src/types.ts | 1 + 4 files changed, 11 insertions(+) diff --git a/src/transports/base.ts b/src/transports/base.ts index 1d982e7..ad9848c 100644 --- a/src/transports/base.ts +++ b/src/transports/base.ts @@ -38,4 +38,6 @@ export abstract class BasePeer implements Peer { logger, }: { onDone?: any; onError?: (...args: any[]) => void; logger: Logger }, ): void; + + abstract end(): void; } diff --git a/src/transports/tcp.ts b/src/transports/tcp.ts index 7ba0dfc..dcf9bb3 100644 --- a/src/transports/tcp.ts +++ b/src/transports/tcp.ts @@ -6,6 +6,8 @@ import * as console from "console"; import { BasePeer } from "#transports/base.js"; export class TcpPeer extends BasePeer implements Peer { + declare _socket: net.Socket; + sendMessage(message: Uint8Array): void { this._socket.write(message); } @@ -59,6 +61,9 @@ export class TcpPeer extends BasePeer implements Peer { this._socket.on("error", onError); } } + end(): void { + this._socket.end(); + } public static async connect(uri: URL): Promise { const host = uri.hostname; diff --git a/src/transports/webSocket.ts b/src/transports/webSocket.ts index 96e0b3d..da2b657 100644 --- a/src/transports/webSocket.ts +++ b/src/transports/webSocket.ts @@ -42,6 +42,9 @@ export class WebSocketPeer extends BasePeer implements Peer { this._socket.addEventListener("error", onError); } } + end(): void { + this._socket.end(); + } public static async connect(uri: URL): Promise { return new Promise((resolve, reject) => { diff --git a/src/types.ts b/src/types.ts index 2e3e2b0..b23118d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -28,6 +28,7 @@ export interface Peer { ): void; renderLocationUri(): string; + end(): void; } export type PeerConstructorOptions = { socket: any; uris?: URL[] };