*Update dist

This commit is contained in:
Derrick Hammer 2023-02-01 08:15:36 -05:00
parent 9d14579ae9
commit a1afba565e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 40 additions and 31 deletions

1
dist/index.d.ts vendored
View File

@ -7,6 +7,7 @@ export default class HyperswarmWeb extends EventEmitter {
private _activeRelay; private _activeRelay;
private _discovery; private _discovery;
private _queuedEmActions; private _queuedEmActions;
private _connectionMutex;
constructor(opts?: any); constructor(opts?: any);
ready(): Promise<void>; ready(): Promise<void>;
private ensureConnection; private ensureConnection;

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

@ -1 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,uBAAuB,CAAC;AAW5C,OAAO,UAAU,MAAM,YAAY,CAAC;AAEpC,OAAO,YAAY,MAAM,eAAe,CAAC;AAEzC,MAAM,CAAC,OAAO,OAAO,aAAc,SAAQ,YAAY;IACrD,OAAO,CAAC,QAAQ,CAAM;IACtB,OAAO,CAAC,OAAO,CAA0B;IACzC,OAAO,CAAC,YAAY,CAAa;IACjC,OAAO,CAAC,UAAU,CAAsB;IACxC,OAAO,CAAC,gBAAgB,CAAuB;gBACnC,IAAI,GAAE,GAAQ;IAO1B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;YAIR,gBAAgB;YA0DhB,iBAAiB;IAYzB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC;IAQ7D,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAEY,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI7C,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAUpC,WAAW,IAAI,IAAI;IAI1B,EAAE,CACA,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,G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AAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC;IAQ7D,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAEY,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI7C,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAUpC,WAAW,IAAI,IAAI;IAI1B,EAAE,CACA,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GACjC,UAAU;IAGb,WAAW,CACT,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GACjC,IAAI;IAIP,GAAG,CACD,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GACjC,UAAU;IAIb,cAAc,CACZ,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GACjC,IAAI;IAGP,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO;IAIzD,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;IAI1E,OAAO,CAAC,qBAAqB;IAS7B,OAAO,CAAC,qBAAqB;CAO9B"}

68
dist/index.js vendored
View File

@ -8,12 +8,14 @@ import { load } from "@lumeweb/libkernel-universal";
import Hyperswarm from "hyperswarm"; import Hyperswarm from "hyperswarm";
import randomNumber from "random-number-csprng"; import randomNumber from "random-number-csprng";
import EventEmitter from "eventemitter2"; import EventEmitter from "eventemitter2";
import { Mutex } from "async-mutex";
export default class HyperswarmWeb extends EventEmitter { export default class HyperswarmWeb extends EventEmitter {
_options; _options;
_relays = new Set(); _relays = new Set();
_activeRelay; _activeRelay;
_discovery; _discovery;
_queuedEmActions = []; _queuedEmActions = [];
_connectionMutex = new Mutex();
constructor(opts = {}) { constructor(opts = {}) {
super(); super();
opts.custodial = false; opts.custodial = false;
@ -25,45 +27,51 @@ export default class HyperswarmWeb extends EventEmitter {
} }
async ensureConnection() { async ensureConnection() {
const logErr = (await load()).logErr; const logErr = (await load()).logErr;
await this._connectionMutex.waitForUnlock();
this._connectionMutex.acquire();
if (this._activeRelay) { if (this._activeRelay) {
return; return;
} }
const relays = this.relays; const relays = this.relays;
do { if (relays.length > 0) {
const index = relays.length > 1 ? await randomNumber(0, relays.length - 1) : 0; do {
const relay = relays[index]; const index = relays.length > 1 ? await randomNumber(0, relays.length - 1) : 0;
let ret; const relay = relays[index];
try { let ret;
ret = await this._discovery.discover(relay); try {
} ret = await this._discovery.discover(relay);
catch (e) { }
logErr(e); catch (e) {
relays.splice(index, 1); logErr(e);
continue; relays.splice(index, 1);
} continue;
if (!ret) { }
relays.splice(index, 1); if (!ret) {
continue; relays.splice(index, 1);
} continue;
ret = ret; }
const connection = `wss://${ret.host}:${ret.port}`; ret = ret;
if (!(await this.isServerAvailable(connection))) { const connection = `wss://${ret.host}:${ret.port}`;
relays.splice(index, 1); if (!(await this.isServerAvailable(connection))) {
continue; relays.splice(index, 1);
} continue;
this._activeRelay = new Hyperswarm({ }
dht: new DhtNode(new Stream(true, new WebSocket(connection)), this._options), this._activeRelay = new Hyperswarm({
keyPair: this._options.keyPair, dht: new DhtNode(new Stream(true, new WebSocket(connection)), this._options),
}); keyPair: this._options.keyPair,
this._activeRelay.on("close", () => { });
this._activeRelay = undefined; this._activeRelay.on("close", () => {
}); this._activeRelay = undefined;
} while (relays.length > 0 && !this._activeRelay); });
} while (relays.length > 0 && !this._activeRelay);
}
if (!this._activeRelay) { if (!this._activeRelay) {
this._connectionMutex.release();
throw new Error("Failed to find an available relay"); throw new Error("Failed to find an available relay");
} }
this._processQueuedActions(); this._processQueuedActions();
await this._activeRelay.dht.ready(); await this._activeRelay.dht.ready();
this._connectionMutex.release();
} }
async isServerAvailable(connection) { async isServerAvailable(connection) {
return new Promise((resolve) => { return new Promise((resolve) => {