Skip to content

Commit

Permalink
Merge pull request #418 from docknetwork/feat/address-as-array
Browse files Browse the repository at this point in the history
Allow to provide node address as array for fallbacks
  • Loading branch information
cykoder authored Apr 22, 2024
2 parents 14efc17 + 7dcf476 commit 1b14eb9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion example/dock-did.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function registerNewDID() {
// Initialize Dock API, connect to the node and start working with it
// It will create a new DID with a key, then update the key to another one and then remove the DID
dock.init({
address: FullNodeEndpoint,
address: [FullNodeEndpoint],
})
.then(() => {
const account = dock.keyring.addFromUri(TestAccountURI);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/sdk",
"version": "8.1.1",
"version": "8.1.2",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
32 changes: 22 additions & 10 deletions src/dock-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,18 @@ export default class DockAPI {
}

this.address = address || this.address;
if (
this.address
&& this.address.indexOf('wss://') === -1
&& this.address.indexOf('https://') === -1
) {
console.warn(`WARNING: Using non-secure endpoint: ${this.address}`);
}

const addressArray = Array.isArray(this.address) ? this.address : [this.address];

addressArray.forEach((addr) => {
if (
typeof addr === 'string'
&& addr.indexOf('wss://') === -1
&& addr.indexOf('https://') === -1
) {
console.warn(`WARNING: Using non-secure endpoint: ${addr}`);
}
});

// If RPC methods given, use them else set it to empty object.
let rpc = chainRpc || {};
Expand All @@ -99,10 +104,17 @@ export default class DockAPI {
rpc = Object.assign(rpc, PoaRpcDefs);
}

const isWebsocket = this.address && this.address.indexOf('http') === -1;
// NOTE: The correct way to handle would be to raise error if a mix of URL types is provided or accept a preference of websocket vs http.
const addressStr = addressArray[0];
const isWebsocket = addressStr && addressStr.indexOf('http') === -1;

if (!isWebsocket && addressArray.length > 1) {
console.warn('WARNING: HTTP connections do not support more than one URL, ignoring rest');
}

const provider = isWebsocket
? new WsProvider(this.address)
: new HttpProvider(this.address);
? new WsProvider(addressArray)
: new HttpProvider(addressStr);

const apiOptions = {
provider,
Expand Down

0 comments on commit 1b14eb9

Please sign in to comment.