2022-08-03 16:05:10 +00:00
|
|
|
import { Buffer } from "buffer";
|
2023-07-01 09:35:48 +00:00
|
|
|
import { Client, factory } from "@lumeweb/libkernel/module";
|
|
|
|
import { DataFn, ErrTuple, hexToBuf, logErr } from "@lumeweb/libkernel";
|
2023-02-18 01:04:21 +00:00
|
|
|
import { blake2b } from "@noble/hashes/blake2b";
|
2023-03-19 18:55:23 +00:00
|
|
|
import b4a from "b4a";
|
2022-07-20 01:38:29 +00:00
|
|
|
|
2023-02-17 14:25:58 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import Backoff from "backoff.js";
|
2023-04-05 07:52:53 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import Protomux from "protomux";
|
2023-07-22 10:11:18 +00:00
|
|
|
import { UnsubscribeFn } from "emittery";
|
2023-02-17 02:47:49 +00:00
|
|
|
|
2023-02-01 12:54:20 +00:00
|
|
|
export class SwarmClient extends Client {
|
|
|
|
private useDefaultSwarm: boolean;
|
|
|
|
private id: number = 0;
|
2023-02-17 02:47:49 +00:00
|
|
|
private _autoReconnect: boolean;
|
2023-02-17 14:25:58 +00:00
|
|
|
private _connectBackoff: any;
|
2023-02-17 02:47:49 +00:00
|
|
|
|
|
|
|
private _ready?: Promise<void>;
|
2023-03-19 18:02:50 +00:00
|
|
|
private _connectionListener?: [
|
|
|
|
sendUpdate: DataFn,
|
2023-07-01 09:35:48 +00:00
|
|
|
response: Promise<ErrTuple>,
|
2023-03-19 18:02:50 +00:00
|
|
|
];
|
2022-07-20 01:38:29 +00:00
|
|
|
|
2023-02-18 00:35:56 +00:00
|
|
|
private _topics: Set<Uint8Array> = new Set<Uint8Array>();
|
2023-03-19 14:48:11 +00:00
|
|
|
private _sockets: Map<number, Socket> = new Map<number, Socket>();
|
2023-02-18 00:35:56 +00:00
|
|
|
|
2023-03-18 17:55:33 +00:00
|
|
|
get dht() {
|
|
|
|
const self = this;
|
|
|
|
return {
|
|
|
|
async ready() {
|
|
|
|
return self.ready();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-01 09:35:48 +00:00
|
|
|
constructor(module: string, useDefaultDht = true, autoReconnect = false) {
|
|
|
|
super(module);
|
2023-02-01 12:54:20 +00:00
|
|
|
this.useDefaultSwarm = useDefaultDht;
|
2023-02-17 02:47:49 +00:00
|
|
|
this._autoReconnect = autoReconnect;
|
2023-02-17 14:25:58 +00:00
|
|
|
this._connectBackoff = new Backoff({
|
|
|
|
strategy: "fibo",
|
2023-02-17 16:28:07 +00:00
|
|
|
maxAttempts: Number.MAX_SAFE_INTEGER,
|
2023-02-17 02:47:49 +00:00
|
|
|
});
|
2023-03-19 18:03:24 +00:00
|
|
|
|
|
|
|
this._connectBackoff.on("retry", (error: any) => {
|
2023-07-01 09:35:48 +00:00
|
|
|
logErr(error);
|
2023-03-19 18:03:24 +00:00
|
|
|
});
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
2022-07-20 22:03:10 +00:00
|
|
|
|
2023-02-06 10:25:00 +00:00
|
|
|
get swarm(): number | undefined {
|
|
|
|
return this.useDefaultSwarm ? undefined : this.id;
|
|
|
|
}
|
|
|
|
|
2023-02-01 12:54:20 +00:00
|
|
|
public async connect(pubkey: string | Uint8Array): Promise<Socket> {
|
|
|
|
if (typeof pubkey === "string") {
|
|
|
|
const buf = hexToBuf(pubkey);
|
|
|
|
pubkey = this.handleErrorOrReturn(buf);
|
2022-07-20 22:03:10 +00:00
|
|
|
}
|
2023-02-01 12:54:20 +00:00
|
|
|
|
2023-03-19 18:55:23 +00:00
|
|
|
let existing = Array.from(this._sockets.values()).filter((socket) => {
|
|
|
|
return b4a.equals(socket.remotePublicKey, pubkey as Uint8Array);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (existing.length) {
|
|
|
|
return existing[0] as Socket;
|
|
|
|
}
|
2023-02-01 12:54:20 +00:00
|
|
|
|
2023-03-19 18:55:23 +00:00
|
|
|
throw new Error("not implemented");
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
2023-04-05 07:52:53 +00:00
|
|
|
|
2023-02-01 13:47:19 +00:00
|
|
|
async init(): Promise<ErrTuple> {
|
2023-02-17 13:31:24 +00:00
|
|
|
return await this.callModuleReturn("init", { swarm: this.swarm });
|
2023-02-01 13:47:19 +00:00
|
|
|
}
|
2023-04-05 07:52:53 +00:00
|
|
|
|
2023-02-06 08:13:32 +00:00
|
|
|
async ready(): Promise<void> {
|
2023-02-17 02:47:49 +00:00
|
|
|
if (this._ready) {
|
|
|
|
return this._ready;
|
|
|
|
}
|
|
|
|
|
2023-02-17 13:49:35 +00:00
|
|
|
this._listen();
|
2023-02-17 02:47:49 +00:00
|
|
|
|
2023-02-17 13:20:02 +00:00
|
|
|
this._ready = this.callModuleReturn("ready", { swarm: this.swarm });
|
|
|
|
|
|
|
|
await this._ready;
|
|
|
|
|
2023-02-17 02:47:49 +00:00
|
|
|
this._ready = undefined;
|
2023-02-18 00:35:56 +00:00
|
|
|
|
|
|
|
for (const topic of this._topics) {
|
|
|
|
await this.join(topic);
|
|
|
|
}
|
2023-02-17 02:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async start(): Promise<void> {
|
2023-02-17 21:50:05 +00:00
|
|
|
await this._connectBackoff.run(() => this.init());
|
2023-02-17 14:25:58 +00:00
|
|
|
|
2023-02-17 13:08:43 +00:00
|
|
|
await this.ready();
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
2022-07-20 22:03:10 +00:00
|
|
|
|
2023-02-17 13:49:35 +00:00
|
|
|
private async _listen() {
|
2023-03-19 18:02:50 +00:00
|
|
|
if (!this._connectionListener) {
|
|
|
|
this._connectionListener = this.connectModule(
|
|
|
|
"listenConnections",
|
|
|
|
{ swarm: this.swarm },
|
|
|
|
async (socketId: any) => {
|
|
|
|
const socket =
|
2023-04-06 14:08:23 +00:00
|
|
|
this._sockets.get(socketId) ?? (await createSocket(socketId, this));
|
2023-03-19 18:02:50 +00:00
|
|
|
|
|
|
|
socket.on("close", () => {
|
|
|
|
this._sockets.delete(socketId);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!this._sockets.has(socketId)) {
|
|
|
|
this._sockets.set(socketId, socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit("connection", socket);
|
2023-07-01 09:35:48 +00:00
|
|
|
},
|
2023-03-19 18:02:50 +00:00
|
|
|
);
|
|
|
|
}
|
2023-03-19 15:14:31 +00:00
|
|
|
|
2023-03-19 18:02:50 +00:00
|
|
|
await this._connectionListener[1];
|
|
|
|
this._connectionListener = undefined;
|
2023-02-17 14:25:58 +00:00
|
|
|
this.start();
|
2023-02-17 13:49:35 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 16:05:10 +00:00
|
|
|
public async addRelay(pubkey: string): Promise<void> {
|
2023-02-01 12:54:20 +00:00
|
|
|
return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm });
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
2022-08-03 16:04:53 +00:00
|
|
|
|
2022-08-03 16:05:10 +00:00
|
|
|
public async removeRelay(pubkey: string): Promise<void> {
|
2023-02-01 12:54:20 +00:00
|
|
|
return this.callModuleReturn("removeRelay", { pubkey, swarm: this.swarm });
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
2022-08-03 16:04:53 +00:00
|
|
|
|
2022-08-03 16:05:10 +00:00
|
|
|
public async clearRelays(): Promise<void> {
|
2023-02-01 12:54:20 +00:00
|
|
|
return this.callModuleReturn("clearRelays", { swarm: this.swarm });
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
2023-04-05 07:52:53 +00:00
|
|
|
|
2022-08-14 11:32:37 +00:00
|
|
|
public async getRelays(): Promise<string[]> {
|
2023-02-01 12:54:20 +00:00
|
|
|
return this.callModuleReturn("getRelays", { swarm: this.swarm });
|
2022-08-03 16:32:46 +00:00
|
|
|
}
|
2023-02-01 19:06:13 +00:00
|
|
|
|
2023-02-18 01:04:21 +00:00
|
|
|
public async join(topic: Buffer | Uint8Array | string): Promise<void> {
|
|
|
|
if (typeof topic === "string") {
|
2023-02-18 02:03:42 +00:00
|
|
|
topic = blake2b(topic, { dkLen: 32 });
|
2023-02-18 01:04:21 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 00:35:56 +00:00
|
|
|
this._topics.add(topic);
|
2023-02-01 19:06:13 +00:00
|
|
|
this.callModule("join", { id: this.id, topic });
|
|
|
|
}
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
2022-07-20 22:03:10 +00:00
|
|
|
|
2023-02-06 10:25:00 +00:00
|
|
|
interface SocketRawStream {
|
|
|
|
remoteHost: string;
|
|
|
|
remotePort: number;
|
|
|
|
remoteFamily: string;
|
|
|
|
}
|
|
|
|
|
2023-02-01 12:54:20 +00:00
|
|
|
export class Socket extends Client {
|
2022-08-03 16:05:10 +00:00
|
|
|
private id: number;
|
2023-07-22 00:26:12 +00:00
|
|
|
private eventUpdates: { [event: string]: Map<Function, DataFn> } = {};
|
2022-08-03 16:05:10 +00:00
|
|
|
|
2023-04-06 13:57:04 +00:00
|
|
|
private swarm: SwarmClient;
|
2023-04-06 14:17:16 +00:00
|
|
|
private userData?: any = null;
|
2023-04-06 13:57:04 +00:00
|
|
|
|
2023-07-01 09:35:48 +00:00
|
|
|
constructor(module: string, id: number, swarm: SwarmClient) {
|
|
|
|
super(module);
|
2022-08-03 16:05:10 +00:00
|
|
|
this.id = id;
|
2023-04-06 13:57:04 +00:00
|
|
|
this.swarm = swarm;
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
2023-02-06 10:25:00 +00:00
|
|
|
|
|
|
|
private _remotePublicKey?: Uint8Array;
|
|
|
|
|
|
|
|
get remotePublicKey(): Uint8Array {
|
|
|
|
return this._remotePublicKey as Uint8Array;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _rawStream?: Uint8Array;
|
|
|
|
|
|
|
|
get rawStream(): Uint8Array {
|
|
|
|
return this._rawStream as Uint8Array;
|
|
|
|
}
|
|
|
|
|
|
|
|
async setup() {
|
|
|
|
let info = await this.callModuleReturn("socketGetInfo", { id: this.id });
|
|
|
|
|
|
|
|
this._remotePublicKey = info.remotePublicKey;
|
|
|
|
this._rawStream = info.rawStream;
|
2023-07-01 20:06:03 +00:00
|
|
|
await this.swarm.emit("setup", this);
|
2023-04-05 07:52:53 +00:00
|
|
|
}
|
2023-04-08 20:11:03 +00:00
|
|
|
|
2023-07-22 10:11:18 +00:00
|
|
|
on(event: any, listener: any): UnsubscribeFn {
|
2023-07-22 10:03:44 +00:00
|
|
|
const parentOn = super.on(event, listener);
|
2023-07-22 00:26:12 +00:00
|
|
|
if (this.eventUpdates[event]?.has(listener)) {
|
2023-07-22 10:03:44 +00:00
|
|
|
return parentOn;
|
2023-07-22 00:26:12 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 12:54:20 +00:00
|
|
|
const [update, promise] = this.connectModule(
|
2023-02-06 08:35:04 +00:00
|
|
|
"socketListenEvent",
|
2023-02-06 08:11:42 +00:00
|
|
|
{ id: this.id, event: event },
|
2022-08-03 16:05:10 +00:00
|
|
|
(data: any) => {
|
2023-02-06 08:11:42 +00:00
|
|
|
this.emit(event, data);
|
2023-07-01 09:35:48 +00:00
|
|
|
},
|
2022-08-03 16:05:10 +00:00
|
|
|
);
|
2023-07-22 00:26:12 +00:00
|
|
|
this.trackEvent(event as string, listener, update);
|
2022-08-03 16:05:10 +00:00
|
|
|
|
|
|
|
promise.then(() => {
|
2023-04-08 20:11:03 +00:00
|
|
|
this.off(event as string, listener);
|
2022-08-03 16:05:10 +00:00
|
|
|
});
|
|
|
|
|
2023-07-22 10:03:44 +00:00
|
|
|
return parentOn;
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
|
|
|
|
2023-07-01 20:06:03 +00:00
|
|
|
off(event: any, listener: any): this {
|
2023-07-22 00:26:12 +00:00
|
|
|
if (listener) {
|
|
|
|
const updates = this.eventUpdates[event];
|
|
|
|
updates?.get(listener)?.();
|
|
|
|
updates?.delete(listener);
|
|
|
|
|
|
|
|
return this;
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
2023-07-22 00:26:12 +00:00
|
|
|
|
|
|
|
const updates = [...this.eventUpdates[event as string].values()];
|
|
|
|
this.eventUpdates[event as string] = new Map<Function, DataFn>();
|
|
|
|
updates.forEach((update) => update());
|
2023-07-01 20:06:03 +00:00
|
|
|
super.off(event, listener);
|
2023-07-22 00:26:12 +00:00
|
|
|
|
2023-07-01 20:06:03 +00:00
|
|
|
return this;
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
write(message: string | Buffer): void {
|
2023-07-22 11:11:48 +00:00
|
|
|
this.callModule("socketExists", { id: this.id }).then(
|
|
|
|
([exists]: ErrTuple) => {
|
|
|
|
if (!exists) {
|
|
|
|
logErr("tried to write to closed socket");
|
|
|
|
}
|
|
|
|
this.callModule("socketWrite", { id: this.id, message });
|
|
|
|
},
|
|
|
|
);
|
2023-02-06 08:33:44 +00:00
|
|
|
this.callModule("socketWrite", { id: this.id, message });
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 00:38:28 +00:00
|
|
|
end(): void {
|
2023-02-01 12:54:20 +00:00
|
|
|
this.callModule("socketExists", { id: this.id }).then(
|
2022-08-14 11:32:37 +00:00
|
|
|
([exists]: ErrTuple) => {
|
2023-07-22 11:11:48 +00:00
|
|
|
if (!exists) {
|
|
|
|
logErr("tried to close a closed socket");
|
2022-08-14 11:32:37 +00:00
|
|
|
}
|
2023-07-22 11:11:48 +00:00
|
|
|
this.callModule("socketClose", { id: this.id });
|
2023-07-01 09:35:48 +00:00
|
|
|
},
|
2022-08-14 11:32:37 +00:00
|
|
|
);
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private ensureEvent(event: string): void {
|
|
|
|
if (!(event in this.eventUpdates)) {
|
2023-07-22 00:26:12 +00:00
|
|
|
this.eventUpdates[event] = new Map<Function, DataFn>();
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 00:26:12 +00:00
|
|
|
private trackEvent(event: string, listener: Function, update: DataFn): void {
|
2022-08-03 16:05:10 +00:00
|
|
|
this.ensureEvent(event as string);
|
2023-07-22 00:26:12 +00:00
|
|
|
this.eventUpdates[event].set(listener, update);
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
2022-07-20 01:38:29 +00:00
|
|
|
}
|
2023-02-01 12:54:20 +00:00
|
|
|
|
2023-07-01 09:35:48 +00:00
|
|
|
export const MODULE =
|
2023-07-22 01:43:19 +00:00
|
|
|
"zduN1xKFUkDrPtUdvL9Sih7A6mAcnkhR6JuLtStf3zgCcpEsPyqkmbcoHH";
|
2023-02-01 12:54:20 +00:00
|
|
|
|
|
|
|
export const createClient = factory<SwarmClient>(SwarmClient, MODULE);
|
2023-02-06 10:25:00 +00:00
|
|
|
|
2023-07-22 00:26:12 +00:00
|
|
|
// @ts-ignore
|
2023-02-06 10:25:00 +00:00
|
|
|
const socketFactory = factory<Socket>(Socket, MODULE);
|
|
|
|
const createSocket = async (...args: any): Promise<Socket> => {
|
|
|
|
const socket = socketFactory(...args);
|
|
|
|
|
|
|
|
await socket.setup();
|
|
|
|
|
|
|
|
return socket;
|
|
|
|
};
|