kernel-swarm-client/dist/index.js

147 lines
4.4 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-17 02:48:11 +00:00
import backoff from "backoff";
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;
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;
this._connectBackoff = backoff.fibonacci();
this._connectBackoff.on("ready", () => {
this.start();
});
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-17 13:31:37 +00:00
this._connectBackoff.reset();
2023-02-17 02:48:11 +00:00
}
async start() {
const backoff = () => setImmediate(() => this._connectBackoff.backoff());
try {
await this.init();
}
catch (e) {
this.logErr(e);
backoff();
2023-02-17 13:08:57 +00:00
return;
2023-02-17 02:48:11 +00:00
}
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() {
const connect = this.connectModule("listenConnections", { swarm: this.swarm }, async (socketId) => {
this.emit("connection", await createSocket(socketId));
});
await connect[1];
this._connectBackoff.backoff();
}
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-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-02-01 17:33:30 +00:00
const MODULE = "_A7ClA0mSa1-Pg5c4V3C0H_fnhAFjgccITYT83Euc7t_9A";
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;
};