kernel-swarm-client/dist/index.js

171 lines
5.2 KiB
JavaScript
Raw Normal View History

2023-02-01 12:55:30 +00:00
import { Client, factory } from "@lumeweb/libkernel-universal";
import { hexToBuf } from "@siaweb/libweb";
2023-02-18 01:05:07 +00:00
import { blake2b } from "@noble/hashes/blake2b";
2023-02-17 14:26:10 +00:00
// @ts-ignore
import Backoff from "backoff.js";
2023-02-01 12:55:30 +00:00
export class SwarmClient extends Client {
useDefaultSwarm;
id = 0;
2023-02-17 02:48:11 +00:00
_autoReconnect;
_connectBackoff;
_ready;
2023-03-19 18:03:38 +00:00
_connectionListener;
2023-02-18 00:36:14 +00:00
_topics = new Set();
2023-03-19 14:48:28 +00:00
_sockets = new Map();
2023-03-18 17:55:47 +00:00
get dht() {
const self = this;
return {
async ready() {
return self.ready();
},
};
}
2023-02-17 02:48:11 +00:00
constructor(useDefaultDht = true, autoReconnect = false) {
2023-02-01 12:55:30 +00:00
super();
this.useDefaultSwarm = useDefaultDht;
2023-02-17 02:48:11 +00:00
this._autoReconnect = autoReconnect;
2023-02-17 14:26:10 +00:00
this._connectBackoff = new Backoff({
strategy: "fibo",
2023-02-17 16:28:36 +00:00
maxAttempts: Number.MAX_SAFE_INTEGER,
2023-02-17 02:48:11 +00:00
});
2023-03-19 18:03:38 +00:00
this._connectBackoff.on("retry", (error) => {
this.logErr(error);
});
2022-08-03 16:29:53 +00:00
}
2023-02-06 10:25:32 +00:00
get swarm() {
return this.useDefaultSwarm ? undefined : this.id;
}
2022-07-20 06:13:07 +00:00
async connect(pubkey) {
2023-02-01 12:55:30 +00:00
if (typeof pubkey === "string") {
const buf = hexToBuf(pubkey);
pubkey = this.handleErrorOrReturn(buf);
}
const resp = this.callModuleReturn("connect", {
2022-08-04 01:36:52 +00:00
pubkey,
2023-02-01 12:55:30 +00:00
swarm: this.swarm,
2022-08-04 01:36:52 +00:00
});
2023-02-01 12:55:30 +00:00
return createSocket(resp.id);
2022-07-20 06:13:07 +00:00
}
2023-02-01 13:47:57 +00:00
async init() {
2023-02-17 13:31:37 +00:00
return await this.callModuleReturn("init", { swarm: this.swarm });
2023-02-01 13:47:57 +00:00
}
2022-07-20 06:13:07 +00:00
async ready() {
2023-02-17 02:48:11 +00:00
if (this._ready) {
return this._ready;
}
2023-02-17 13:50:06 +00:00
this._listen();
2023-02-17 13:20:16 +00:00
this._ready = this.callModuleReturn("ready", { swarm: this.swarm });
await this._ready;
2023-02-17 02:48:11 +00:00
this._ready = undefined;
2023-02-18 00:36:14 +00:00
for (const topic of this._topics) {
await this.join(topic);
}
2023-02-17 02:48:11 +00:00
}
async start() {
2023-02-17 21:50:42 +00:00
await this._connectBackoff.run(() => this.init());
2023-02-17 13:08:57 +00:00
await this.ready();
2022-07-20 06:13:07 +00:00
}
2023-02-17 13:50:06 +00:00
async _listen() {
2023-03-19 18:03:38 +00:00
if (!this._connectionListener) {
this._connectionListener = this.connectModule("listenConnections", { swarm: this.swarm }, async (socketId) => {
const socket = this._sockets.get(socketId) ?? (await createSocket(socketId));
socket.on("close", () => {
this._sockets.delete(socketId);
});
if (!this._sockets.has(socketId)) {
this._sockets.set(socketId, socket);
}
this.emit("connection", socket);
2023-03-19 14:48:28 +00:00
});
2023-03-19 18:03:38 +00:00
}
await this._connectionListener[1];
this._connectionListener = undefined;
2023-02-17 14:26:10 +00:00
this.start();
2023-02-17 13:50:06 +00:00
}
2022-07-20 07:27:17 +00:00
async addRelay(pubkey) {
2023-02-01 12:55:30 +00:00
return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm });
2022-07-20 07:27:17 +00:00
}
2022-07-20 22:03:37 +00:00
async removeRelay(pubkey) {
2023-02-01 12:55:30 +00:00
return this.callModuleReturn("removeRelay", { pubkey, swarm: this.swarm });
2022-07-20 22:03:37 +00:00
}
async clearRelays() {
2023-02-01 12:55:30 +00:00
return this.callModuleReturn("clearRelays", { swarm: this.swarm });
2022-08-03 16:29:53 +00:00
}
2022-08-14 11:33:04 +00:00
async getRelays() {
2023-02-01 12:55:30 +00:00
return this.callModuleReturn("getRelays", { swarm: this.swarm });
2022-08-03 16:37:15 +00:00
}
2023-02-01 19:08:26 +00:00
async join(topic) {
2023-02-18 01:05:07 +00:00
if (typeof topic === "string") {
2023-02-18 02:03:50 +00:00
topic = blake2b(topic, { dkLen: 32 });
2023-02-18 01:05:07 +00:00
}
2023-02-18 00:36:14 +00:00
this._topics.add(topic);
2023-02-01 19:06:25 +00:00
this.callModule("join", { id: this.id, topic });
}
2022-07-20 06:13:07 +00:00
}
2023-02-01 12:55:30 +00:00
export class Socket extends Client {
2022-07-20 06:13:07 +00:00
id;
eventUpdates = {};
constructor(id) {
super();
this.id = id;
}
2023-02-06 10:25:32 +00:00
_remotePublicKey;
get remotePublicKey() {
return this._remotePublicKey;
}
_rawStream;
get rawStream() {
return this._rawStream;
}
async setup() {
let info = await this.callModuleReturn("socketGetInfo", { id: this.id });
this._remotePublicKey = info.remotePublicKey;
this._rawStream = info.rawStream;
}
2023-02-06 08:26:19 +00:00
on(event, fn, context) {
2023-02-06 08:50:00 +00:00
const [update, promise] = this.connectModule("socketListenEvent", { id: this.id, event: event }, (data) => {
2023-02-06 08:26:19 +00:00
this.emit(event, data);
2022-07-20 06:13:07 +00:00
});
2023-02-06 08:26:19 +00:00
this.trackEvent(event, update);
2022-07-20 06:13:07 +00:00
promise.then(() => {
2023-02-06 08:26:19 +00:00
this.off(event, fn);
2022-07-20 06:13:07 +00:00
});
2023-02-06 08:26:19 +00:00
return super.on(event, fn, context);
2022-07-20 06:13:07 +00:00
}
2023-02-06 08:26:19 +00:00
off(event, fn, context, once) {
const updates = [...this.eventUpdates[event]];
this.eventUpdates[event] = [];
2022-07-20 06:13:07 +00:00
for (const func of updates) {
2023-02-06 08:26:19 +00:00
func();
2022-07-20 06:13:07 +00:00
}
2023-02-06 08:26:19 +00:00
return super.off(event, fn, context, once);
2022-07-20 06:13:07 +00:00
}
write(message) {
2023-02-06 08:50:00 +00:00
this.callModule("socketWrite", { id: this.id, message });
2022-07-20 06:13:07 +00:00
}
end() {
2023-02-01 12:55:30 +00:00
this.callModule("socketExists", { id: this.id }).then(([exists]) => {
2022-08-04 02:26:40 +00:00
if (exists) {
2023-02-06 08:50:00 +00:00
this.callModule("socketClose", { id: this.id });
2022-08-04 02:26:40 +00:00
}
});
2022-07-20 06:13:07 +00:00
}
ensureEvent(event) {
if (!(event in this.eventUpdates)) {
this.eventUpdates[event] = [];
}
}
trackEvent(event, update) {
this.ensureEvent(event);
this.eventUpdates[event].push(update);
}
}
2023-03-18 18:42:37 +00:00
const MODULE = "_A4zfYX8-ExD-4m2wGpYDVwOg3jCFj2FXthhf-5GHEVgsQ";
2023-02-01 12:55:30 +00:00
export const createClient = factory(SwarmClient, MODULE);
2023-02-06 10:25:32 +00:00
const socketFactory = factory(Socket, MODULE);
const createSocket = async (...args) => {
const socket = socketFactory(...args);
await socket.setup();
return socket;
};