Compare commits

...

3 Commits

8 changed files with 52 additions and 48 deletions

View File

@ -1,3 +1,5 @@
# [0.1.0-develop.10](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.9...v0.1.0-develop.10) (2023-08-31)
# [0.1.0-develop.9](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.8...v0.1.0-develop.9) (2023-08-31) # [0.1.0-develop.9](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.8...v0.1.0-develop.9) (2023-08-31)
# [0.1.0-develop.8](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.7...v0.1.0-develop.8) (2023-08-31) # [0.1.0-develop.8](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.7...v0.1.0-develop.8) (2023-08-31)

4
npm-shrinkwrap.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@lumeweb/libs5", "name": "@lumeweb/libs5",
"version": "0.1.0-develop.9", "version": "0.1.0-develop.10",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@lumeweb/libs5", "name": "@lumeweb/libs5",
"version": "0.1.0-develop.9", "version": "0.1.0-develop.10",
"dependencies": { "dependencies": {
"@noble/curves": "^1.1.0", "@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1", "@noble/hashes": "^1.3.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "@lumeweb/libs5", "name": "@lumeweb/libs5",
"version": "0.1.0-develop.9", "version": "0.1.0-develop.10",
"type": "module", "type": "module",
"main": "lib/index.js", "main": "lib/index.js",
"repository": { "repository": {

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);
} }