Compare commits

...

2 Commits

Author SHA1 Message Date
Derrick Hammer 3ede7224aa
*Update dist 2023-02-16 21:48:11 -05:00
Derrick Hammer db894376eb
*Add connection retry with backoff support 2023-02-16 21:47:49 -05:00
5 changed files with 85 additions and 10 deletions

6
dist/index.d.ts vendored
View File

@ -6,11 +6,15 @@ import type { EventEmitter } from "eventemitter3";
export declare class SwarmClient extends Client {
private useDefaultSwarm;
private id;
constructor(useDefaultDht?: boolean);
private _autoReconnect;
private _connectBackoff;
private _ready?;
constructor(useDefaultDht?: boolean, autoReconnect?: boolean);
get swarm(): number | undefined;
connect(pubkey: string | Uint8Array): Promise<Socket>;
init(): Promise<ErrTuple>;
ready(): Promise<void>;
start(): Promise<void>;
addRelay(pubkey: string): Promise<void>;
removeRelay(pubkey: string): Promise<void>;
clearRelays(): Promise<void>;

2
dist/index.d.ts.map vendored
View File

@ -1 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,MAAM,EAAW,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAoB,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE5D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAElD,qBAAa,WAAY,SAAQ,MAAM;IACrC,OAAO,CAAC,eAAe,CAAU;IACjC,OAAO,CAAC,EAAE,CAAa;gBAEX,aAAa,UAAO;IAKhC,IAAI,KAAK,IAAI,MAAM,GAAG,SAAS,CAE9B;IAEY,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC;IAa5D,IAAI,IAAI,OAAO,CAAC,QAAQ,CAAC;IAGzB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAYf,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIvC,WAAW,CAA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,IAAI,SAAS,IAAI,UAAU,CAE1B;IAEK,KAAK;IAOX,EAAE,CAAC,CAAC,SAAS,YAAY,CAAC,UAAU,CAAC,MAAM,GAAG,MAAM,CAAC,EACnD,KAAK,EAAE,CAAC,EACR,EAAE,EAAE,YAAY,CAAC,aAAa,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC,EAClD,OAAO,CAAC,EAAE,GAAG,GACZ,IAAI;IAiBP,GAAG,CAAC,CAAC,SAAS,YAAY,CAAC,UAAU,CAAC,MAAM,GAAG,MAAM,CAAC,EACpD,KAAK,EAAE,CAAC,EACR,EAAE,CAAC,EAAE,YAAY,CAAC,aAAa,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC,EACnD,OAAO,CAAC,EAAE,GAAG,EACb,IAAI,CAAC,EAAE,OAAO,GACb,IAAI;IASP,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAIrC,GAAG,IAAI,IAAI;IAUX,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,UAAU;CAInB;AAID,eAAO,MAAM,YAAY,+BAA4C,CAAC"}

35
dist/index.js vendored
View File

@ -1,11 +1,20 @@
import { Client, factory } from "@lumeweb/libkernel-universal";
import { hexToBuf } from "@siaweb/libweb";
import backoff from "backoff";
export class SwarmClient extends Client {
useDefaultSwarm;
id = 0;
constructor(useDefaultDht = true) {
_autoReconnect;
_connectBackoff;
_ready;
constructor(useDefaultDht = true, autoReconnect = false) {
super();
this.useDefaultSwarm = useDefaultDht;
this._autoReconnect = autoReconnect;
this._connectBackoff = backoff.fibonacci();
this._connectBackoff.on("ready", () => {
this.start();
});
}
get swarm() {
return this.useDefaultSwarm ? undefined : this.id;
@ -22,13 +31,33 @@ export class SwarmClient extends Client {
return createSocket(resp.id);
}
async init() {
return this.callModuleReturn("init", { swarm: this.swarm });
const ret = await this.callModuleReturn("init", { swarm: this.swarm });
this._connectBackoff.reset();
return ret;
}
async ready() {
await this.callModuleReturn("ready", { swarm: this.swarm });
if (this._ready) {
return this._ready;
}
this._ready = this.callModuleReturn("ready", { swarm: this.swarm });
await this._ready;
this.connectModule("listenConnections", { swarm: this.swarm }, async (socketId) => {
this.emit("connection", await createSocket(socketId));
});
this._ready = undefined;
}
async start() {
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;
}
async addRelay(pubkey) {
return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm });

View File

@ -6,11 +6,13 @@
"dependencies": {
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
"@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git",
"backoff": "^2.5.0",
"eventemitter3": "^5.0.0"
},
"devDependencies": {
"@types/node": "^18.11.19",
"prettier": "^2.8.3",
"@types/backoff": "^2.5.2",
"@types/node": "^18.13.0",
"prettier": "^2.8.4",
"pretty": "^2.0.0",
"typescript": "^4.9.5"
}

View File

@ -4,13 +4,25 @@ import { hexToBuf, DataFn, ErrTuple } from "@siaweb/libweb";
import type { EventEmitter } from "eventemitter3";
import backoff, { Backoff } from "backoff";
export class SwarmClient extends Client {
private useDefaultSwarm: boolean;
private id: number = 0;
private _autoReconnect: boolean;
private _connectBackoff: Backoff;
constructor(useDefaultDht = true) {
private _ready?: Promise<void>;
constructor(useDefaultDht = true, autoReconnect = false) {
super();
this.useDefaultSwarm = useDefaultDht;
this._autoReconnect = autoReconnect;
this._connectBackoff = backoff.fibonacci();
this._connectBackoff.on("ready", () => {
this.start();
});
}
get swarm(): number | undefined {
@ -31,10 +43,19 @@ export class SwarmClient extends Client {
return createSocket(resp.id);
}
async init(): Promise<ErrTuple> {
return this.callModuleReturn("init", { swarm: this.swarm });
const ret = await this.callModuleReturn("init", { swarm: this.swarm });
this._connectBackoff.reset();
return ret;
}
async ready(): Promise<void> {
await this.callModuleReturn("ready", { swarm: this.swarm });
if (this._ready) {
return this._ready;
}
this._ready = this.callModuleReturn("ready", { swarm: this.swarm });
await this._ready;
this.connectModule(
"listenConnections",
@ -43,6 +64,25 @@ export class SwarmClient extends Client {
this.emit("connection", await createSocket(socketId));
}
);
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;
}
public async addRelay(pubkey: string): Promise<void> {