refactor: consolidate to BasePeer class and export BasePeer
This commit is contained in:
parent
4544e4b5c1
commit
bc608d00e3
|
@ -10,6 +10,7 @@ export {
|
|||
createTransportSocket,
|
||||
isTransport,
|
||||
createTransportPeer,
|
||||
BasePeer,
|
||||
} from "./transports/index.js";
|
||||
export type { SignedRegistryEntry };
|
||||
export { NodeId };
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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<string, PeerStatic>();
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -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<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;
|
||||
}
|
||||
|
||||
export class TcpPeer extends BasePeer implements Peer {
|
||||
sendMessage(message: Uint8Array): void {
|
||||
this._socket.write(message);
|
||||
}
|
||||
|
|
|
@ -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<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;
|
||||
}
|
||||
|
||||
export class WebSocketPeer extends BasePeer implements Peer {
|
||||
sendMessage(message: Uint8Array): void {
|
||||
this._socket.send(message);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue