*Update dist

This commit is contained in:
Derrick Hammer 2023-04-08 21:21:14 -04:00
parent 4829b48058
commit f7a5628a91
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 157 additions and 166 deletions

2
dist/index.d.ts vendored
View File

@ -47,6 +47,6 @@ export declare class Socket extends Client {
private ensureEvent; private ensureEvent;
private trackEvent; private trackEvent;
} }
export declare const MODULE = "_AVKgzVYC8Sb_qiTA6kw5BDzQ4Ch-8D4sldQJl8dXF9oTw"; export declare const MODULE = "_AV0jHIhooJYvu8a9_9os4aTHQP-QlDymCtvbFCuPcpetw";
export declare const createClient: (...args: any) => SwarmClient; export declare const createClient: (...args: any) => SwarmClient;
//# sourceMappingURL=index.d.ts.map //# sourceMappingURL=index.d.ts.map

321
dist/index.js vendored
View File

@ -6,183 +6,174 @@ import b4a from "b4a";
import Backoff from "backoff.js"; import Backoff from "backoff.js";
import { Mutex } from "async-mutex"; import { Mutex } from "async-mutex";
export class SwarmClient extends Client { export class SwarmClient extends Client {
useDefaultSwarm; useDefaultSwarm;
id = 0; id = 0;
_autoReconnect; _autoReconnect;
_connectBackoff; _connectBackoff;
_ready; _ready;
_connectionListener; _connectionListener;
_topics = new Set(); _topics = new Set();
_sockets = new Map(); _sockets = new Map();
get dht() { get dht() {
const self = this; const self = this;
return { return {
async ready() { async ready() {
return self.ready(); return self.ready();
}, },
}; };
}
constructor(useDefaultDht = true, autoReconnect = false) {
super();
this.useDefaultSwarm = useDefaultDht;
this._autoReconnect = autoReconnect;
this._connectBackoff = new Backoff({
strategy: "fibo",
maxAttempts: Number.MAX_SAFE_INTEGER,
});
this._connectBackoff.on("retry", (error) => {
this.logErr(error);
});
}
get swarm() {
return this.useDefaultSwarm ? undefined : this.id;
}
async connect(pubkey) {
if (typeof pubkey === "string") {
const buf = hexToBuf(pubkey);
pubkey = this.handleErrorOrReturn(buf);
} }
let existing = Array.from(this._sockets.values()).filter((socket) => { constructor(useDefaultDht = true, autoReconnect = false) {
return b4a.equals(socket.remotePublicKey, pubkey); super();
}); this.useDefaultSwarm = useDefaultDht;
if (existing.length) { this._autoReconnect = autoReconnect;
return existing[0]; this._connectBackoff = new Backoff({
strategy: "fibo",
maxAttempts: Number.MAX_SAFE_INTEGER,
});
this._connectBackoff.on("retry", (error) => {
this.logErr(error);
});
} }
throw new Error("not implemented"); get swarm() {
} return this.useDefaultSwarm ? undefined : this.id;
async init() {
return await this.callModuleReturn("init", { swarm: this.swarm });
}
async ready() {
if (this._ready) {
return this._ready;
} }
this._listen(); async connect(pubkey) {
this._ready = this.callModuleReturn("ready", { swarm: this.swarm }); if (typeof pubkey === "string") {
await this._ready; const buf = hexToBuf(pubkey);
this._ready = undefined; pubkey = this.handleErrorOrReturn(buf);
for (const topic of this._topics) {
await this.join(topic);
}
}
async start() {
await this._connectBackoff.run(() => this.init());
await this.ready();
}
async _listen() {
if (!this._connectionListener) {
this._connectionListener = this.connectModule(
"listenConnections",
{ swarm: this.swarm },
async (socketId) => {
const socket =
this._sockets.get(socketId) ?? (await createSocket(socketId, this));
socket.on("close", () => {
this._sockets.delete(socketId);
});
if (!this._sockets.has(socketId)) {
this._sockets.set(socketId, socket);
}
this.emit("connection", socket);
} }
); let existing = Array.from(this._sockets.values()).filter((socket) => {
return b4a.equals(socket.remotePublicKey, pubkey);
});
if (existing.length) {
return existing[0];
}
throw new Error("not implemented");
} }
await this._connectionListener[1]; async init() {
this._connectionListener = undefined; return await this.callModuleReturn("init", { swarm: this.swarm });
this.start(); }
} async ready() {
async addRelay(pubkey) { if (this._ready) {
return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm }); return this._ready;
} }
async removeRelay(pubkey) { this._listen();
return this.callModuleReturn("removeRelay", { pubkey, swarm: this.swarm }); this._ready = this.callModuleReturn("ready", { swarm: this.swarm });
} await this._ready;
async clearRelays() { this._ready = undefined;
return this.callModuleReturn("clearRelays", { swarm: this.swarm }); for (const topic of this._topics) {
} await this.join(topic);
async getRelays() { }
return this.callModuleReturn("getRelays", { swarm: this.swarm }); }
} async start() {
async join(topic) { await this._connectBackoff.run(() => this.init());
if (typeof topic === "string") { await this.ready();
topic = blake2b(topic, { dkLen: 32 }); }
async _listen() {
if (!this._connectionListener) {
this._connectionListener = this.connectModule("listenConnections", { swarm: this.swarm }, async (socketId) => {
const socket = this._sockets.get(socketId) ?? (await createSocket(socketId, this));
socket.on("close", () => {
this._sockets.delete(socketId);
});
if (!this._sockets.has(socketId)) {
this._sockets.set(socketId, socket);
}
this.emit("connection", socket);
});
}
await this._connectionListener[1];
this._connectionListener = undefined;
this.start();
}
async addRelay(pubkey) {
return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm });
}
async removeRelay(pubkey) {
return this.callModuleReturn("removeRelay", { pubkey, swarm: this.swarm });
}
async clearRelays() {
return this.callModuleReturn("clearRelays", { swarm: this.swarm });
}
async getRelays() {
return this.callModuleReturn("getRelays", { swarm: this.swarm });
}
async join(topic) {
if (typeof topic === "string") {
topic = blake2b(topic, { dkLen: 32 });
}
this._topics.add(topic);
this.callModule("join", { id: this.id, topic });
} }
this._topics.add(topic);
this.callModule("join", { id: this.id, topic });
}
} }
export class Socket extends Client { export class Socket extends Client {
id; id;
eventUpdates = {}; eventUpdates = {};
syncMutex = new Mutex(); syncMutex = new Mutex();
swarm; swarm;
userData = null; userData = null;
constructor(id, swarm) { constructor(id, swarm) {
super(); super();
this.id = id; this.id = id;
this.swarm = swarm; this.swarm = swarm;
}
_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;
await this.swarm.emitAsync("setup", this);
}
on(event, listener, options) {
const [update, promise] = this.connectModule(
"socketListenEvent",
{ id: this.id, event: event },
(data) => {
this.emit(event, data);
}
);
this.trackEvent(event, update);
promise.then(() => {
this.off(event, listener);
});
return super.on(event, listener, options);
}
off(event, listener) {
const updates = [...this.eventUpdates[event]];
this.eventUpdates[event] = [];
for (const func of updates) {
func();
} }
return super.off(event, listener); _remotePublicKey;
} get remotePublicKey() {
write(message) { return this._remotePublicKey;
this.callModule("socketWrite", { id: this.id, message }); }
} _rawStream;
end() { get rawStream() {
this.callModule("socketExists", { id: this.id }).then(([exists]) => { return this._rawStream;
if (exists) { }
this.callModule("socketClose", { id: this.id }); async setup() {
} let info = await this.callModuleReturn("socketGetInfo", { id: this.id });
}); this._remotePublicKey = info.remotePublicKey;
} this._rawStream = info.rawStream;
ensureEvent(event) { await this.swarm.emitAsync("setup", this);
if (!(event in this.eventUpdates)) { }
this.eventUpdates[event] = []; on(event, listener, options) {
const [update, promise] = this.connectModule("socketListenEvent", { id: this.id, event: event }, (data) => {
this.emit(event, data);
});
this.trackEvent(event, update);
promise.then(() => {
this.off(event, listener);
});
return super.on(event, listener, options);
}
off(event, listener) {
const updates = [...this.eventUpdates[event]];
this.eventUpdates[event] = [];
for (const func of updates) {
func();
}
return super.off(event, listener);
}
write(message) {
this.callModule("socketWrite", { id: this.id, message });
}
end() {
this.callModule("socketExists", { id: this.id }).then(([exists]) => {
if (exists) {
this.callModule("socketClose", { id: this.id });
}
});
}
ensureEvent(event) {
if (!(event in this.eventUpdates)) {
this.eventUpdates[event] = [];
}
}
trackEvent(event, update) {
this.ensureEvent(event);
this.eventUpdates[event].push(update);
} }
}
trackEvent(event, update) {
this.ensureEvent(event);
this.eventUpdates[event].push(update);
}
} }
export const MODULE = "_AVbH2uOdd4s4hypKB3QqaP1kxJO_Q5m6y4hsEro6dpVJQ"; export const MODULE = "_AV0jHIhooJYvu8a9_9os4aTHQP-QlDymCtvbFCuPcpetw";
export const createClient = factory(SwarmClient, MODULE); export const createClient = factory(SwarmClient, MODULE);
const socketFactory = factory(Socket, MODULE); const socketFactory = factory(Socket, MODULE);
const createSocket = async (...args) => { const createSocket = async (...args) => {
const socket = socketFactory(...args); const socket = socketFactory(...args);
await socket.setup(); await socket.setup();
return socket; return socket;
}; };