refactor: consolidate to BasePeer class and export BasePeer

This commit is contained in:
Derrick Hammer 2023-08-31 17:32:37 -04:00
parent 4544e4b5c1
commit bc608d00e3
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 47 additions and 45 deletions

View File

@ -10,6 +10,7 @@ export {
createTransportSocket, createTransportSocket,
isTransport, isTransport,
createTransportPeer, createTransportPeer,
BasePeer,
} from "./transports/index.js"; } from "./transports/index.js";
export type { SignedRegistryEntry }; export type { SignedRegistryEntry };
export { NodeId }; export { NodeId };

39
src/transports/base.ts Normal file
View File

@ -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<URL>;
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<void>,
{
onDone,
onError,
logger,
}: { onDone?: any; onError?: (...args: any[]) => void; logger: Logger },
): void;
}

View File

@ -3,6 +3,7 @@ import { TcpPeer } from "#transports/tcp.js";
import { WebSocketPeer } from "#transports/webSocket.js"; import { WebSocketPeer } from "#transports/webSocket.js";
import { PeerStatic } from "#types.js"; import { PeerStatic } from "#types.js";
import isNode from "detect-node"; import isNode from "detect-node";
import { BasePeer } from "#transports/base.js";
const transports = new Map<string, PeerStatic>(); const transports = new Map<string, PeerStatic>();
export function registerTransport(type: string, transport: PeerStatic) { export function registerTransport(type: string, transport: PeerStatic) {
@ -37,6 +38,8 @@ export function createTransportPeer(
return new transport(socket, connectionUris); return new transport(socket, connectionUris);
} }
export { BasePeer };
if (isNode) { if (isNode) {
registerTransport("tcp", TcpPeer); registerTransport("tcp", TcpPeer);
} }

View File

@ -1,32 +1,11 @@
import { Logger, Peer } from "../types.js"; import { Logger, Peer } from "../types.js";
import NodeId from "../nodeId.js";
import * as net from "net"; import * as net from "net";
import { URL } from "url"; import { URL } from "url";
import { decodeEndian } from "../util.js"; import { decodeEndian } from "../util.js";
import * as console from "console"; import * as console from "console";
import { BasePeer } from "#transports/base.js";
export class TcpPeer implements Peer { export class TcpPeer extends BasePeer implements Peer {
connectionUris: Array<URL>;
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;
}
sendMessage(message: Uint8Array): void { sendMessage(message: Uint8Array): void {
this._socket.write(message); this._socket.write(message);
} }

View File

@ -2,29 +2,9 @@ import { Logger, Peer } from "../types.js";
import NodeId from "../nodeId.js"; import NodeId from "../nodeId.js";
import { URL } from "url"; import { URL } from "url";
import { WebSocket } from "ws"; import { WebSocket } from "ws";
import { BasePeer } from "#transports/base.js";
export class WebSocketPeer implements Peer { export class WebSocketPeer extends BasePeer implements Peer {
connectionUris: Array<URL>;
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;
}
sendMessage(message: Uint8Array): void { sendMessage(message: Uint8Array): void {
this._socket.send(message); this._socket.send(message);
} }