From bc608d00e36cfc0e8c8e3b261c2be5f3decb2ae3 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 31 Aug 2023 17:32:37 -0400 Subject: [PATCH] refactor: consolidate to BasePeer class and export BasePeer --- src/index.ts | 1 + src/transports/base.ts | 39 +++++++++++++++++++++++++++++++++++++ src/transports/index.ts | 3 +++ src/transports/tcp.ts | 25 ++---------------------- src/transports/webSocket.ts | 24 ++--------------------- 5 files changed, 47 insertions(+), 45 deletions(-) create mode 100644 src/transports/base.ts diff --git a/src/index.ts b/src/index.ts index 00fc522..5e0fb39 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ export { createTransportSocket, isTransport, createTransportPeer, + BasePeer, } from "./transports/index.js"; export type { SignedRegistryEntry }; export { NodeId }; diff --git a/src/transports/base.ts b/src/transports/base.ts new file mode 100644 index 0000000..f34bfe2 --- /dev/null +++ b/src/transports/base.ts @@ -0,0 +1,39 @@ +import { Logger, Peer } from "#types.js"; +import { URL } from "url"; +import NodeId from "#nodeId.js"; + +export abstract class BasePeer implements Peer { + connectionUris: Array; + isConnected: boolean = false; + challenge: Uint8Array; + protected _socket: any; + + constructor(_socket: any, connectionUris: URL[]) { + this.connectionUris = connectionUris.map((uri) => new URL(uri.toString())); + this.challenge = new Uint8Array(); + this._socket = _socket; + } + + private _id?: NodeId; + + get id(): NodeId { + return this._id as NodeId; + } + + set id(value: NodeId) { + this._id = value; + } + + abstract sendMessage(message: Uint8Array); + + abstract renderLocationUri(): string; + + abstract listenForMessages( + callback: (event: any) => Promise, + { + onDone, + onError, + logger, + }: { onDone?: any; onError?: (...args: any[]) => void; logger: Logger }, + ): void; +} diff --git a/src/transports/index.ts b/src/transports/index.ts index b8f5df5..bcc8ff8 100644 --- a/src/transports/index.ts +++ b/src/transports/index.ts @@ -3,6 +3,7 @@ import { TcpPeer } from "#transports/tcp.js"; import { WebSocketPeer } from "#transports/webSocket.js"; import { PeerStatic } from "#types.js"; import isNode from "detect-node"; +import { BasePeer } from "#transports/base.js"; const transports = new Map(); export function registerTransport(type: string, transport: PeerStatic) { @@ -37,6 +38,8 @@ export function createTransportPeer( return new transport(socket, connectionUris); } +export { BasePeer }; + if (isNode) { registerTransport("tcp", TcpPeer); } diff --git a/src/transports/tcp.ts b/src/transports/tcp.ts index 1a0ffe0..7ba0dfc 100644 --- a/src/transports/tcp.ts +++ b/src/transports/tcp.ts @@ -1,32 +1,11 @@ import { Logger, Peer } from "../types.js"; -import NodeId from "../nodeId.js"; import * as net from "net"; import { URL } from "url"; import { decodeEndian } from "../util.js"; import * as console from "console"; +import { BasePeer } from "#transports/base.js"; -export class TcpPeer implements Peer { - connectionUris: Array; - isConnected: boolean = false; - challenge: Uint8Array; - private _socket: net.Socket; - - constructor(_socket: net.Socket, connectionUris: URL[]) { - this.connectionUris = connectionUris.map((uri) => new URL(uri.toString())); - this.challenge = new Uint8Array(); - this._socket = _socket; - } - - private _id?: NodeId; - - get id(): NodeId { - return this._id as NodeId; - } - - set id(value: NodeId) { - this._id = value; - } - +export class TcpPeer extends BasePeer implements Peer { sendMessage(message: Uint8Array): void { this._socket.write(message); } diff --git a/src/transports/webSocket.ts b/src/transports/webSocket.ts index 0db4b9e..f245aa2 100644 --- a/src/transports/webSocket.ts +++ b/src/transports/webSocket.ts @@ -2,29 +2,9 @@ import { Logger, Peer } from "../types.js"; import NodeId from "../nodeId.js"; import { URL } from "url"; import { WebSocket } from "ws"; +import { BasePeer } from "#transports/base.js"; -export class WebSocketPeer implements Peer { - connectionUris: Array; - isConnected: boolean = false; - challenge: Uint8Array; - private _socket: WebSocket; - - constructor(_socket: WebSocket, connectionUris: URL[]) { - this.connectionUris = connectionUris.map((uri) => new URL(uri.toString())); - this.challenge = new Uint8Array(); // Initialize as needed - this._socket = _socket; - } - - private _id?: NodeId; - - get id(): NodeId { - return this._id as NodeId; - } - - set id(value: NodeId) { - this._id = value; - } - +export class WebSocketPeer extends BasePeer implements Peer { sendMessage(message: Uint8Array): void { this._socket.send(message); }