2022-08-03 16:05:10 +00:00
|
|
|
import { Buffer } from "buffer";
|
2023-02-01 12:54:20 +00:00
|
|
|
import { Client, factory } from "@lumeweb/libkernel-universal";
|
|
|
|
import { hexToBuf, DataFn, ErrTuple } from "@siaweb/libweb";
|
2022-07-20 01:38:29 +00:00
|
|
|
|
2023-02-06 08:11:42 +00:00
|
|
|
import type { EventEmitter } from "eventemitter3";
|
|
|
|
|
2023-02-17 02:47:49 +00:00
|
|
|
import backoff, { Backoff } from "backoff";
|
|
|
|
|
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;
|
|
|
|
private _connectBackoff: Backoff;
|
|
|
|
|
|
|
|
private _ready?: Promise<void>;
|
2022-07-20 01:38:29 +00:00
|
|
|
|
2023-02-17 02:47:49 +00:00
|
|
|
constructor(useDefaultDht = true, autoReconnect = false) {
|
2023-02-01 12:54:20 +00:00
|
|
|
super();
|
|
|
|
this.useDefaultSwarm = useDefaultDht;
|
2023-02-17 02:47:49 +00:00
|
|
|
this._autoReconnect = autoReconnect;
|
|
|
|
this._connectBackoff = backoff.fibonacci();
|
|
|
|
|
|
|
|
this._connectBackoff.on("ready", () => {
|
|
|
|
this.start();
|
|
|
|
});
|
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
|
|
|
|
|
|
|
const resp = this.callModuleReturn("connect", {
|
|
|
|
pubkey,
|
|
|
|
swarm: this.swarm,
|
|
|
|
}) as any;
|
|
|
|
|
|
|
|
return createSocket(resp.id);
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
2023-02-01 13:47:19 +00:00
|
|
|
async init(): Promise<ErrTuple> {
|
2023-02-17 02:47:49 +00:00
|
|
|
const ret = await this.callModuleReturn("init", { swarm: this.swarm });
|
|
|
|
this._connectBackoff.reset();
|
|
|
|
|
|
|
|
return ret;
|
2023-02-01 13:47:19 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._ready = this.callModuleReturn("ready", { swarm: this.swarm });
|
|
|
|
|
|
|
|
await this._ready;
|
2023-02-06 08:13:32 +00:00
|
|
|
|
|
|
|
this.connectModule(
|
|
|
|
"listenConnections",
|
|
|
|
{ swarm: this.swarm },
|
2023-02-06 10:25:00 +00:00
|
|
|
async (socketId: any) => {
|
|
|
|
this.emit("connection", await createSocket(socketId));
|
2023-02-06 08:13:32 +00:00
|
|
|
}
|
|
|
|
);
|
2023-02-17 02:47:49 +00:00
|
|
|
|
|
|
|
this._ready = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
async start(): Promise<void> {
|
|
|
|
let ready = this.ready();
|
|
|
|
|
|
|
|
const backoff = () => setImmediate(() => this._connectBackoff.backoff());
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.init();
|
|
|
|
} catch (e) {
|
|
|
|
this.logErr(e);
|
|
|
|
backoff();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.once("close", backoff);
|
|
|
|
|
|
|
|
await ready;
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
2022-07-20 22:03:10 +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
|
|
|
}
|
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-01 19:08:11 +00:00
|
|
|
public async join(topic: Buffer): Promise<void> {
|
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;
|
|
|
|
private eventUpdates: { [event: string]: DataFn[] } = {};
|
|
|
|
|
|
|
|
constructor(id: number) {
|
|
|
|
super();
|
|
|
|
this.id = id;
|
|
|
|
}
|
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-02-06 08:11:42 +00:00
|
|
|
on<T extends EventEmitter.EventNames<string | symbol>>(
|
|
|
|
event: T,
|
|
|
|
fn: EventEmitter.EventListener<string | symbol, T>,
|
|
|
|
context?: any
|
|
|
|
): this {
|
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);
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
|
|
|
);
|
2023-02-06 08:11:42 +00:00
|
|
|
this.trackEvent(event as string, update);
|
2022-08-03 16:05:10 +00:00
|
|
|
|
|
|
|
promise.then(() => {
|
2023-02-06 08:11:42 +00:00
|
|
|
this.off(event as string, fn);
|
2022-08-03 16:05:10 +00:00
|
|
|
});
|
|
|
|
|
2023-02-06 08:11:42 +00:00
|
|
|
return super.on(event, fn, context) as this;
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 08:11:42 +00:00
|
|
|
off<T extends EventEmitter.EventNames<string | symbol>>(
|
|
|
|
event: T,
|
|
|
|
fn?: EventEmitter.EventListener<string | symbol, T>,
|
|
|
|
context?: any,
|
|
|
|
once?: boolean
|
|
|
|
): this {
|
|
|
|
const updates = [...this.eventUpdates[event as string]];
|
|
|
|
this.eventUpdates[event as string] = [];
|
2022-08-03 16:05:10 +00:00
|
|
|
for (const func of updates) {
|
2023-02-06 08:11:42 +00:00
|
|
|
func();
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
2023-02-06 08:11:42 +00:00
|
|
|
return super.off(event, fn, context, once);
|
2022-08-03 16:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
write(message: string | Buffer): void {
|
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) => {
|
|
|
|
if (exists) {
|
2023-02-06 08:49:48 +00:00
|
|
|
this.callModule("socketClose", { id: this.id });
|
2022-08-14 11:32:37 +00:00
|
|
|
}
|
2022-08-04 02:22:40 +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)) {
|
|
|
|
this.eventUpdates[event] = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private trackEvent(event: string, update: DataFn): void {
|
|
|
|
this.ensureEvent(event as string);
|
|
|
|
this.eventUpdates[event].push(update);
|
|
|
|
}
|
2022-07-20 01:38:29 +00:00
|
|
|
}
|
2023-02-01 12:54:20 +00:00
|
|
|
|
2023-02-01 17:33:17 +00:00
|
|
|
const MODULE = "_A7ClA0mSa1-Pg5c4V3C0H_fnhAFjgccITYT83Euc7t_9A";
|
2023-02-01 12:54:20 +00:00
|
|
|
|
|
|
|
export const createClient = factory<SwarmClient>(SwarmClient, MODULE);
|
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;
|
|
|
|
};
|