Compare commits

..

2 Commits

Author SHA1 Message Date
Derrick Hammer a1afba565e
*Update dist 2023-02-01 08:15:36 -05:00
Derrick Hammer 9d14579ae9
*Add a mutex lock so we are only ever attempting 1 bootup at a time
*Only try to process relays when we have one
2023-02-01 08:15:16 -05:00
6 changed files with 104 additions and 71 deletions

1
dist/index.d.ts vendored
View File

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

View File

@ -18,6 +18,7 @@
"@hyperswarm/dht-relay": "^0.3.0",
"@lumeweb/kernel-peer-discovery-client": "git+https://git.lumeweb.com/LumeWeb/kernel-peer-discovery-client.git",
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
"async-mutex": "^0.4.0",
"eventemitter2": "^6.4.9",
"hyperswarm": "^4.3.7"
}

View File

@ -6,6 +6,7 @@ specifiers:
'@lumeweb/libkernel-universal': git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git
'@types/random-number-csprng': ^1.0.0
'@types/ws': ^8.5.4
async-mutex: ^0.4.0
esbuild: ^0.14.54
eventemitter2: ^6.4.9
hyperswarm: ^4.3.7
@ -15,8 +16,9 @@ specifiers:
dependencies:
'@hyperswarm/dht-relay': 0.3.0
'@lumeweb/kernel-peer-discovery-client': git.lumeweb.com/LumeWeb/kernel-peer-discovery-client/e5ed06e1df9f60bee10980ff8b337fe2990f4b52
'@lumeweb/libkernel-universal': git.lumeweb.com/LumeWeb/libkernel-universal/fb377db59fd24761d140eb98f49166ea1cc7cc39
'@lumeweb/kernel-peer-discovery-client': git.lumeweb.com/LumeWeb/kernel-peer-discovery-client/ec966974f6898dc3dc8d484df786bfe940421131
'@lumeweb/libkernel-universal': git.lumeweb.com/LumeWeb/libkernel-universal/0bc5807c6afdcc95ed960a6ba2c304388fa67f28
async-mutex: 0.4.0
eventemitter2: 6.4.9
hyperswarm: 4.3.7
@ -107,6 +109,12 @@ packages:
'@types/node': 18.11.18
dev: true
/async-mutex/0.4.0:
resolution: {integrity: sha512-eJFZ1YhRR8UN8eBLoNzcDPcy/jqjsg6I1AP+KvWQX80BqOSW1oJPJXDylPUEeMr2ZQvHgnQ//Lp6f3RQ1zI7HA==}
dependencies:
tslib: 2.5.0
dev: false
/b4a/1.6.1:
resolution: {integrity: sha512-AsKjNhz72yxteo/0EtQEiwkMUgk/tGmycXlbG4g3Ard2/ULtNLUykGOkeK0egmN27h0xMAhb76jYccW+XTBExA==}
dev: false
@ -716,6 +724,10 @@ packages:
resolution: {integrity: sha512-SVqEcMZBsZF9mA78rjzCrYrUs37LMJk3ShZ851ygZYW1cMeIjs9mL57KO6Iv5mmjSQnOe/29/VAfGXo+oRCiVw==}
dev: false
/tslib/2.5.0:
resolution: {integrity: sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==}
dev: false
/typescript/4.9.5:
resolution: {integrity: sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==}
engines: {node: '>=4.2.0'}
@ -749,13 +761,13 @@ packages:
resolution: {integrity: sha512-FIr/DEeoHfj7ftfylnoFt3rAIRoWXpx2AoDfrT2qD2wtp7Dp+COajvs/Icb7uHqRW9m60f5iXZwdsJJO3kvb7w==}
dev: false
git.lumeweb.com/LumeWeb/kernel-peer-discovery-client/e5ed06e1df9f60bee10980ff8b337fe2990f4b52:
resolution: {commit: e5ed06e1df9f60bee10980ff8b337fe2990f4b52, repo: https://git.lumeweb.com/LumeWeb/kernel-peer-discovery-client.git, type: git}
git.lumeweb.com/LumeWeb/kernel-peer-discovery-client/ec966974f6898dc3dc8d484df786bfe940421131:
resolution: {commit: ec966974f6898dc3dc8d484df786bfe940421131, repo: https://git.lumeweb.com/LumeWeb/kernel-peer-discovery-client.git, type: git}
name: '@lumeweb/kernel-peer-discovery-client'
version: 0.1.0
dependencies:
'@hyperswarm/dht-relay': 0.3.0
'@lumeweb/libkernel-universal': git.lumeweb.com/LumeWeb/libkernel-universal/fb377db59fd24761d140eb98f49166ea1cc7cc39
'@lumeweb/libkernel-universal': git.lumeweb.com/LumeWeb/libkernel-universal/0bc5807c6afdcc95ed960a6ba2c304388fa67f28
'@lumeweb/peer-discovery': git.lumeweb.com/LumeWeb/peer-discovery/d2bd926275103d60fdd3c7c432eae5c278f49261
'@siaweb/libweb': git.lumeweb.com/LumeWeb/libsiaweb/5e6cdba3e7d9a4b94e21ddcd5f2b5138fb440ee8
hyperswarm: 4.3.7
@ -763,12 +775,13 @@ packages:
libkmodule: 0.2.53
dev: false
git.lumeweb.com/LumeWeb/libkernel-universal/fb377db59fd24761d140eb98f49166ea1cc7cc39:
resolution: {commit: fb377db59fd24761d140eb98f49166ea1cc7cc39, repo: https://git.lumeweb.com/LumeWeb/libkernel-universal.git, type: git}
git.lumeweb.com/LumeWeb/libkernel-universal/0bc5807c6afdcc95ed960a6ba2c304388fa67f28:
resolution: {commit: 0bc5807c6afdcc95ed960a6ba2c304388fa67f28, repo: https://git.lumeweb.com/LumeWeb/libkernel-universal.git, type: git}
name: '@lumeweb/libkernel-universal'
version: 0.1.0
dependencies:
'@siaweb/libweb': git.lumeweb.com/LumeWeb/libsiaweb/5e6cdba3e7d9a4b94e21ddcd5f2b5138fb440ee8
eventemitter2: 6.4.9
libkernel: 0.1.48
libkmodule: 0.2.53
dev: false

View File

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