Compare commits

..

No commits in common. "a1afba565e47eec17ce4443735f86571b88f2d41" and "5a49d74b77d8f6adbbc32ddefd5706231eb1f29d" have entirely different histories.

6 changed files with 71 additions and 104 deletions

1
dist/index.d.ts vendored
View File

@ -7,7 +7,6 @@ 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;AAGzC,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;IAE/C,OAAO,CAAC,gBAAgB,CAAsB;gBAClC,IAAI,GAAE,GAAQ;IAO1B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;YAIR,gBAAgB;YAiEhB,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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M,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,14 +8,12 @@ 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;
@ -27,51 +25,45 @@ 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;
if (relays.length > 0) { do {
do { const index = relays.length > 1 ? await randomNumber(0, relays.length - 1) : 0;
const index = relays.length > 1 ? await randomNumber(0, relays.length - 1) : 0; const relay = relays[index];
const relay = relays[index]; let ret;
let ret; try {
try { ret = await this._discovery.discover(relay);
ret = await this._discovery.discover(relay); }
} catch (e) {
catch (e) { logErr(e);
logErr(e); relays.splice(index, 1);
relays.splice(index, 1); continue;
continue; }
} if (!ret) {
if (!ret) { relays.splice(index, 1);
relays.splice(index, 1); continue;
continue; }
} ret = ret;
ret = ret; const connection = `wss://${ret.host}:${ret.port}`;
const connection = `wss://${ret.host}:${ret.port}`; if (!(await this.isServerAvailable(connection))) {
if (!(await this.isServerAvailable(connection))) { relays.splice(index, 1);
relays.splice(index, 1); continue;
continue; }
} this._activeRelay = new Hyperswarm({
this._activeRelay = new Hyperswarm({ dht: new DhtNode(new Stream(true, new WebSocket(connection)), this._options),
dht: new DhtNode(new Stream(true, new WebSocket(connection)), this._options), keyPair: this._options.keyPair,
keyPair: this._options.keyPair, });
}); this._activeRelay.on("close", () => {
this._activeRelay.on("close", () => { this._activeRelay = undefined;
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) => {

View File

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

View File

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

View File

@ -13,7 +13,6 @@ 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 {
private _options: any; private _options: any;
@ -21,8 +20,6 @@ export default class HyperswarmWeb extends EventEmitter {
private _activeRelay: Hyperswarm; private _activeRelay: Hyperswarm;
private _discovery: PeerDiscoveryClient; private _discovery: PeerDiscoveryClient;
private _queuedEmActions: [string, any][] = []; private _queuedEmActions: [string, any][] = [];
private _connectionMutex: Mutex = new Mutex();
constructor(opts: any = {}) { constructor(opts: any = {}) {
super(); super();
opts.custodial = false; opts.custodial = false;
@ -37,66 +34,59 @@ export default class HyperswarmWeb extends EventEmitter {
private async ensureConnection(): Promise<any> { private async ensureConnection(): Promise<any> {
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;
if (relays.length > 0) { do {
do { const index =
const index = relays.length > 1 ? await randomNumber(0, relays.length - 1) : 0;
relays.length > 1 ? await randomNumber(0, relays.length - 1) : 0; const relay = relays[index];
const relay = relays[index];
let ret; let ret;
try { try {
ret = await this._discovery.discover(relay); ret = await this._discovery.discover(relay);
} catch (e) { } catch (e) {
logErr(e); logErr(e);
relays.splice(index, 1); relays.splice(index, 1);
continue; continue;
} }
if (!ret) { if (!ret) {
relays.splice(index, 1); relays.splice(index, 1);
continue; 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))) { if (!(await this.isServerAvailable(connection))) {
relays.splice(index, 1); relays.splice(index, 1);
continue; continue;
} }
this._activeRelay = new Hyperswarm({ this._activeRelay = new Hyperswarm({
dht: new DhtNode( dht: new DhtNode(
new Stream(true, new WebSocket(connection)), new Stream(true, new WebSocket(connection)),
this._options this._options
), ),
keyPair: this._options.keyPair, keyPair: this._options.keyPair,
}); });
this._activeRelay.on("close", () => { this._activeRelay.on("close", () => {
this._activeRelay = undefined; 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();
} }
private async isServerAvailable(connection: string): Promise<boolean> { private async isServerAvailable(connection: string): Promise<boolean> {