diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index 5157b6372..ebd0906bc 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -136,7 +136,9 @@ class LokiMessageAPI { } async refreshSendingSwarm(pubKey, timestamp) { - const freshNodes = await lokiSnodeAPI.refreshSwarmNodesForPubKey(this.ourKey); + const freshNodes = await lokiSnodeAPI.refreshSwarmNodesForPubKey( + this.ourKey + ); this.sendingData[timestamp].swarm = freshNodes; this.sendingData[timestamp].hasFreshList = true; return true; @@ -197,7 +199,9 @@ class LokiMessageAPI { if (result === false) { // this means the node we asked for is likely down log.warn( - `loki_message:::_sendToNode - Try #${successiveFailures}/${MAX_ACCEPTABLE_FAILURES} ${targetNode.ip}:${targetNode.port} failed` + `loki_message:::_sendToNode - Try #${successiveFailures}/${MAX_ACCEPTABLE_FAILURES} ${ + targetNode.ip + }:${targetNode.port} failed` ); successiveFailures += 1; // eslint-disable-next-line no-continue @@ -428,7 +432,7 @@ class LokiMessageAPI { // floor or ceil probably doesn't matter, since it's likely always uneven const poolSize = Math.floor(nodes.length / numConnections, 10); const pools = []; - while(nodes.length) { + while (nodes.length) { const poolList = nodes.splice(0, poolSize); const byAddressObj = poolList.reduce((result, node) => { // eslint-disable-next-line no-param-reassign @@ -444,14 +448,16 @@ class LokiMessageAPI { for (let i = 0; i < numConnections; i += 1) { promises.push( // eslint-disable-next-line more/no-then - this._openRetrieveConnection(pools[i], stopPolling, callback).then(() => { - unresolved -= 1; - log.info( - 'loki_message:::startLongPolling - There are', - unresolved, - 'open retrieve connections left' - ); - }) + this._openRetrieveConnection(pools[i], stopPolling, callback).then( + () => { + unresolved -= 1; + log.info( + 'loki_message:::startLongPolling - There are', + unresolved, + 'open retrieve connections left' + ); + } + ) ); } diff --git a/js/modules/loki_primitives.js b/js/modules/loki_primitives.js index 3a8a315b6..1c55aa0b1 100644 --- a/js/modules/loki_primitives.js +++ b/js/modules/loki_primitives.js @@ -2,7 +2,7 @@ // was timeoutDelay const sleepFor = ms => new Promise(resolve => setTimeout(resolve, ms)); -let log +let log; function configure(options = {}) { ({ log } = options); } @@ -45,8 +45,10 @@ async function allowOnlyOneAtATime(name, process, timeout) { let innerRetVal; try { innerRetVal = await process(); - } catch(e) { - log.error(`loki_snodes:::allowOnlyOneAtATime - error ${e.code} ${e.message}`); + } catch (e) { + log.error( + `loki_snodes:::allowOnlyOneAtATime - error ${e.code} ${e.message}` + ); throw e; } // clear timeout timer @@ -74,7 +76,6 @@ async function allowOnlyOneAtATime(name, process, timeout) { } function abortableIterator(array, iterator) { - let abortIteration = false; // for the control promise @@ -93,22 +94,24 @@ function abortableIterator(array, iterator) { const accum = []; return { - start: async (serially) => { + start: async serially => { let item = destructableList.pop(); - while(item && !abortIteration) { + while (item && !abortIteration) { // console.log('iterating on item', item); if (serially) { try { // eslint-disable-next-line no-await-in-loop accum.push(await iterator(item)); - } catch(e) { - log.error(`loki_snodes:::abortableIterator - error ${e.code} ${e.message}`); + } catch (e) { + log.error( + `loki_snodes:::abortableIterator - error ${e.code} ${e.message}` + ); throw e; } } else { accum.push(iterator(item)); } - item = destructableList.pop() + item = destructableList.pop(); } return accum; }, @@ -120,7 +123,7 @@ function abortableIterator(array, iterator) { */ controlResolveFunctor(); }, - } + }; } module.exports = { @@ -129,4 +132,4 @@ module.exports = { allowOnlyOneAtATime, abortableIterator, firstTrue, -} +}; diff --git a/js/modules/loki_rpc.js b/js/modules/loki_rpc.js index 8bcb31a19..6fb92d54a 100644 --- a/js/modules/loki_rpc.js +++ b/js/modules/loki_rpc.js @@ -279,7 +279,6 @@ const sendToProxy = async (options = {}, targetNode, retryNumber = 0) => { // relay is fine but destination is not good if (response.status === 504) { - const pRetryNumber = retryNumber + 1; if (pRetryNumber > 3) { log.warn( diff --git a/js/modules/loki_snode_api.js b/js/modules/loki_snode_api.js index 05be061cd..cc6b54f0a 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -18,9 +18,9 @@ const RANDOM_SNODES_TO_USE_FOR_PUBKEY_SWARM = 3; const SEED_NODE_RETRIES = 3; const SNODE_VERSION_RETRIES = 3; - // findMatchingSnode(search)(current) -const findMatchingSnode = search => current => current.ip === search.ip && current.port === search.port; +const findMatchingSnode = search => current => + current.ip === search.ip && current.port === search.port; // just get the filtered list async function tryGetSnodeListFromLokidSeednode( @@ -384,8 +384,8 @@ class LokiSnodeAPI { // not cacheable because we write to this.randomSnodePool elsewhere getNodesMinVersion(minVersion) { - return this.randomSnodePool.filter(node => - node.version && semver.gt(node.version, minVersion) + return this.randomSnodePool.filter( + node => node.version && semver.gt(node.version, minVersion) ); } @@ -397,7 +397,11 @@ class LokiSnodeAPI { try { await this.refreshRandomPool(); } catch (e) { - log.error(`loki_snode:::getRandomProxySnodeAddress - error ${e.code} ${e.message}`); + log.error( + `loki_snode:::getRandomProxySnodeAddress - error ${e.code} ${ + e.message + }` + ); throw e; } if (this.randomSnodePool.length === 0) { @@ -407,7 +411,9 @@ class LokiSnodeAPI { const goodPool = this.getNodesMinVersion('2.0.1'); if (!goodPool.length) { // FIXME: retry - log.warn(`loki_snode:::getRandomProxySnodeAddress - no good versions yet`); + log.warn( + `loki_snode:::getRandomProxySnodeAddress - no good versions yet` + ); return false; } // FIXME: _.sample? @@ -430,14 +436,18 @@ class LokiSnodeAPI { process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1'; const data = await result.json(); if (data.version) { - const foundNodeIdx = this.randomSnodePool.findIndex(findMatchingSnode(node)); + const foundNodeIdx = this.randomSnodePool.findIndex( + findMatchingSnode(node) + ); if (foundNodeIdx !== -1) { this.randomSnodePool[foundNodeIdx].version = data.version; } else { // maybe already marked bad... - log.warn(`loki_snode:::_getVersion - can't find ${node.ip}:${ - node.port - } in randomSnodePool`); + log.warn( + `loki_snode:::_getVersion - can't find ${node.ip}:${ + node.port + } in randomSnodePool` + ); } } return data.version; @@ -465,11 +475,15 @@ class LokiSnodeAPI { `on ${node.ip}:${node.port} retrying in 1s` ); await primitives.sleepFor(1000); - await this._getVersion(node, {...options, retries: retries + 1 }); + await this._getVersion(node, { ...options, retries: retries + 1 }); } else { this.markRandomNodeUnreachable(node); const randomNodesLeft = this.getRandomPoolLength(); - log.warn(`loki_snode:::_getVersion - failing to get version for ${node.ip}:${node.port}, removing, leaving ${randomNodesLeft} in the randomPool`) + log.warn( + `loki_snode:::_getVersion - failing to get version for ${node.ip}:${ + node.port + }, removing, leaving ${randomNodesLeft} in the randomPool` + ); } // maybe throw? return false; @@ -483,11 +497,13 @@ class LokiSnodeAPI { // const verionStart = Date.now(); // const total = this.randomSnodePool.length; // const noticeEvery = parseInt(total / 10, 10); - const loop = primitives.abortableIterator(this.randomSnodePool, async (node) => { - // count += 1; - try { - await this._getVersion(node); - /* + const loop = primitives.abortableIterator( + this.randomSnodePool, + async node => { + // count += 1; + try { + await this._getVersion(node); + /* if (count % noticeEvery === 0) { // give stats const diff = Date.now() - verionStart; @@ -502,11 +518,16 @@ class LokiSnodeAPI { }); } */ - } catch (e) { - log.error('loki_snode:::_getAllVerionsForRandomSnodePool - error', e.code, e.message); - throw(e); + } catch (e) { + log.error( + 'loki_snode:::_getAllVerionsForRandomSnodePool - error', + e.code, + e.message + ); + throw e; + } } - }); + ); // make abortable accessible outside this scope this.stopGetAllVersionPromiseControl = loop.stop; await loop.start(true); @@ -542,11 +563,7 @@ class LokiSnodeAPI { // start polling versions but no need to await it this._getAllVerionsForRandomSnodePool(); } catch (e) { - log.warn( - 'loki_snodes:::refreshRandomPool - error', - e.code, - e.message - ); + log.warn('loki_snodes:::refreshRandomPool - error', e.code, e.message); /* log.error( 'loki_snodes:::refreshRandomPoolPromise - Giving up trying to contact seed node' @@ -592,9 +609,9 @@ class LokiSnodeAPI { } try { await conversation.updateSwarmNodes(filteredNodes); - } catch(e) { + } catch (e) { log.error(`loki_snodes:::unreachableNode - error ${e.code} ${e.message}`); - throw(e); + throw e; } return filteredNodes; } @@ -605,7 +622,11 @@ class LokiSnodeAPI { async updateLastHash(snodeAddress, hash, expiresAt) { // FIXME: handle rejections - await window.Signal.Data.updateLastHash({ snode: snodeAddress, hash, expiresAt }); + await window.Signal.Data.updateLastHash({ + snode: snodeAddress, + hash, + expiresAt, + }); } // called by loki_message:::sendMessage & loki_message:::startLongPolling @@ -615,18 +636,24 @@ class LokiSnodeAPI { const swarmNodes = [...conversation.get('swarmNodes')]; // always? include lashHash - await Promise.all(Object.keys(swarmNodes).map(async j => { - const node = swarmNodes[j]; - // FIXME make a batch function call - const lastHash = await window.Signal.Data.getLastHashBySnode( - node.address - ); - log.debug(`loki_snode:::getSwarmNodesForPubKey - ${j} ${node.ip}:${node.port} hash ${lastHash} for ${node.address}`); - swarmNodes[j] = { - ...node, - lastHash, - }; - })); + await Promise.all( + Object.keys(swarmNodes).map(async j => { + const node = swarmNodes[j]; + // FIXME make a batch function call + const lastHash = await window.Signal.Data.getLastHashBySnode( + node.address + ); + log.debug( + `loki_snode:::getSwarmNodesForPubKey - ${j} ${node.ip}:${ + node.port + } hash ${lastHash} for ${node.address}` + ); + swarmNodes[j] = { + ...node, + lastHash, + }; + }) + ); return swarmNodes; } catch (e) { @@ -643,7 +670,9 @@ class LokiSnodeAPI { await conversation.updateSwarmNodes(filteredNodes); return filteredNodes; } catch (e) { - log.error(`loki_snodes:::updateSwarmNodes - error ${e.code} ${e.message}`); + log.error( + `loki_snodes:::updateSwarmNodes - error ${e.code} ${e.message}` + ); throw new window.textsecure.ReplayableError({ message: 'Could not get conversation', });