ssb-conn-query
Advanced tools
Comparing version 0.4.6 to 1.0.0
153
lib/index.js
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var Time = require("./queries/time"); | ||
var Health = require("./queries/health"); | ||
var Sorting = require("./queries/sorting"); | ||
var ConnQuery = (function () { | ||
function ConnQuery(db, hub, staging) { | ||
const Time = require("./queries/time"); | ||
const Health = require("./queries/health"); | ||
const Sorting = require("./queries/sorting"); | ||
class ConnQuery { | ||
constructor(db, hub, staging) { | ||
this.db = db; | ||
@@ -38,13 +11,9 @@ this.hub = hub; | ||
} | ||
ConnQuery.prototype._hubEntryToPeer = function (_a) { | ||
var _b = __read(_a, 2), address = _b[0], hubData = _b[1]; | ||
var stagingEntry = Array.from(this.staging.entries()).find(function (_a) { | ||
var _b = __read(_a, 1), addr = _b[0]; | ||
return addr === address; | ||
}); | ||
var peer = this.db.has(address) | ||
? [address, __assign({ pool: 'db' }, this.db.get(address))] | ||
_hubEntryToPeer([address, hubData]) { | ||
const stagingEntry = Array.from(this.staging.entries()).find(([addr]) => addr === address); | ||
const peer = this.db.has(address) | ||
? [address, { pool: 'db', ...this.db.get(address) }] | ||
: !!stagingEntry | ||
? [address, __assign({ pool: 'staging' }, stagingEntry[1])] | ||
: [address, __assign({ pool: 'hub' }, hubData)]; | ||
? [address, { pool: 'staging', ...stagingEntry[1] }] | ||
: [address, { pool: 'hub', ...hubData }]; | ||
if (hubData.key && !peer[1].key) { | ||
@@ -54,55 +23,35 @@ peer[1].key = hubData.key; | ||
return peer; | ||
}; | ||
ConnQuery.prototype.peersAll = function () { | ||
} | ||
peersAll() { | ||
return this.peersConnectable('dbAndStaging').concat(this.peersInConnection()); | ||
}; | ||
ConnQuery.prototype.peersConnected = function () { | ||
var _this = this; | ||
} | ||
peersConnected() { | ||
return Array.from(this.hub.entries()) | ||
.filter(function (_a) { | ||
var _b = __read(_a, 2), _address = _b[0], data = _b[1]; | ||
return data.state === 'connected'; | ||
}) | ||
.map(function (e) { return _this._hubEntryToPeer(e); }); | ||
}; | ||
ConnQuery.prototype.peersConnecting = function () { | ||
var _this = this; | ||
.filter(([_address, data]) => data.state === 'connected') | ||
.map((e) => this._hubEntryToPeer(e)); | ||
} | ||
peersConnecting() { | ||
return Array.from(this.hub.entries()) | ||
.filter(function (_a) { | ||
var _b = __read(_a, 2), _address = _b[0], data = _b[1]; | ||
return data.state === 'connecting'; | ||
}) | ||
.map(function (e) { return _this._hubEntryToPeer(e); }); | ||
}; | ||
ConnQuery.prototype.peersInConnection = function () { | ||
var _this = this; | ||
.filter(([_address, data]) => data.state === 'connecting') | ||
.map((e) => this._hubEntryToPeer(e)); | ||
} | ||
peersInConnection() { | ||
return Array.from(this.hub.entries()) | ||
.filter(function (_a) { | ||
var _b = __read(_a, 2), _address = _b[0], data = _b[1]; | ||
return data.state === 'connected' || data.state === 'connecting'; | ||
}) | ||
.map(function (e) { return _this._hubEntryToPeer(e); }); | ||
}; | ||
ConnQuery.prototype.peersConnectable = function (pool) { | ||
var _this = this; | ||
if (pool === void 0) { pool = 'db'; } | ||
var useDB = pool === 'db' || pool === 'dbAndStaging'; | ||
var useStaging = pool === 'staging' || pool === 'dbAndStaging'; | ||
var dbPool = useDB | ||
? Array.from(this.db.entries()).map(function (_a) { | ||
var _b = __read(_a, 2), addr = _b[0], data = _b[1]; | ||
return [ | ||
addr, | ||
__assign({ pool: 'db' }, data), | ||
]; | ||
}) | ||
.filter(([_address, data]) => data.state === 'connected' || data.state === 'connecting') | ||
.map((e) => this._hubEntryToPeer(e)); | ||
} | ||
peersConnectable(pool = 'db') { | ||
const useDB = pool === 'db' || pool === 'dbAndStaging'; | ||
const useStaging = pool === 'staging' || pool === 'dbAndStaging'; | ||
const dbPool = useDB | ||
? Array.from(this.db.entries()).map(([addr, data]) => [ | ||
addr, | ||
{ pool: 'db', ...data }, | ||
]) | ||
: []; | ||
var stagingPool = useStaging | ||
? Array.from(this.staging.entries()).map(function (_a) { | ||
var _b = __read(_a, 2), addr = _b[0], data = _b[1]; | ||
return [ | ||
addr, | ||
__assign({ pool: 'staging' }, data), | ||
]; | ||
}) | ||
const stagingPool = useStaging | ||
? Array.from(this.staging.entries()).map(([addr, data]) => [ | ||
addr, | ||
{ pool: 'staging', ...data }, | ||
]) | ||
: []; | ||
@@ -112,17 +61,15 @@ return [] | ||
.concat(stagingPool) | ||
.filter(function (_a) { | ||
var _b = __read(_a, 1), address = _b[0]; | ||
var state = _this.hub.getState(address); | ||
.filter(([address]) => { | ||
const state = this.hub.getState(address); | ||
return state !== 'connected' && state !== 'connecting'; | ||
}); | ||
}; | ||
ConnQuery.passesExpBackoff = Time.passesExpBackoff; | ||
ConnQuery.passesGroupDebounce = Time.passesGroupDebounce; | ||
ConnQuery.hasNoAttempts = Health.hasNoAttempts; | ||
ConnQuery.hasOnlyFailedAttempts = Health.hasOnlyFailedAttempts; | ||
ConnQuery.hasSuccessfulAttempts = Health.hasSuccessfulAttempts; | ||
ConnQuery.hasPinged = Health.hasPinged; | ||
ConnQuery.sortByStateChange = Sorting.sortByStateChange; | ||
return ConnQuery; | ||
}()); | ||
} | ||
} | ||
ConnQuery.passesExpBackoff = Time.passesExpBackoff; | ||
ConnQuery.passesGroupDebounce = Time.passesGroupDebounce; | ||
ConnQuery.hasNoAttempts = Health.hasNoAttempts; | ||
ConnQuery.hasOnlyFailedAttempts = Health.hasOnlyFailedAttempts; | ||
ConnQuery.hasSuccessfulAttempts = Health.hasSuccessfulAttempts; | ||
ConnQuery.hasPinged = Health.hasPinged; | ||
ConnQuery.sortByStateChange = Sorting.sortByStateChange; | ||
module.exports = ConnQuery; |
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function hasNoAttempts(_a) { | ||
var _b = __read(_a, 2), _addr = _b[0], p = _b[1]; | ||
function hasNoAttempts([_addr, p]) { | ||
return !p.stateChange; | ||
} | ||
exports.hasNoAttempts = hasNoAttempts; | ||
function hasOnlyFailedAttempts(_a) { | ||
var _b = __read(_a, 2), _addr = _b[0], p = _b[1]; | ||
function hasOnlyFailedAttempts([_addr, p]) { | ||
return !!p.stateChange && (!p.duration || p.duration.mean == 0); | ||
} | ||
exports.hasOnlyFailedAttempts = hasOnlyFailedAttempts; | ||
function hasSuccessfulAttempts(_a) { | ||
var _b = __read(_a, 2), _addr = _b[0], p = _b[1]; | ||
function hasSuccessfulAttempts([_addr, p]) { | ||
return !!p.duration && p.duration.mean > 0; | ||
} | ||
exports.hasSuccessfulAttempts = hasSuccessfulAttempts; | ||
function hasPinged(_a) { | ||
var _b = __read(_a, 2), _addr = _b[0], p = _b[1]; | ||
function hasPinged([_addr, p]) { | ||
return !!p.ping && !!p.ping.rtt && p.ping.rtt.mean > 0; | ||
} | ||
exports.hasPinged = hasPinged; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function sortByStateChange(peers) { | ||
return peers.sort(function (a, b) { return a[1].stateChange - b[1].stateChange; }); | ||
return peers.sort((a, b) => a[1].stateChange - b[1].stateChange); | ||
} | ||
exports.sortByStateChange = sortByStateChange; |
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function passesExpBackoff(step, max, timestamp) { | ||
if (max === void 0) { max = Infinity; } | ||
if (timestamp === void 0) { timestamp = Date.now(); } | ||
return function (_a) { | ||
var _b = __read(_a, 2), _addr = _b[0], data = _b[1]; | ||
var prevAttempt = data.stateChange || 0; | ||
var numFailures = data.failure || 0; | ||
var expBackoff = Math.min(Math.pow(2, numFailures) * step, max); | ||
var nextAttempt = prevAttempt + expBackoff; | ||
function passesExpBackoff(step, max = Infinity, timestamp = Date.now()) { | ||
return ([_addr, data]) => { | ||
const prevAttempt = data.stateChange || 0; | ||
const numFailures = data.failure || 0; | ||
const expBackoff = Math.min(Math.pow(2, numFailures) * step, max); | ||
const nextAttempt = prevAttempt + expBackoff; | ||
return nextAttempt < timestamp; | ||
@@ -32,10 +13,6 @@ }; | ||
exports.passesExpBackoff = passesExpBackoff; | ||
function passesGroupDebounce(groupMin, timestamp) { | ||
if (timestamp === void 0) { timestamp = Date.now(); } | ||
return function (group) { | ||
var newestStateChange = group.reduce(function (max, _a) { | ||
var _b = __read(_a, 2), _addr = _b[0], p = _b[1]; | ||
return Math.max(max, p.stateChange || 0); | ||
}, 0); | ||
var minTimeThreshold = newestStateChange + groupMin; | ||
function passesGroupDebounce(groupMin, timestamp = Date.now()) { | ||
return (group) => { | ||
const newestStateChange = group.reduce((max, [_addr, p]) => Math.max(max, p.stateChange || 0), 0); | ||
const minTimeThreshold = newestStateChange + groupMin; | ||
if (timestamp < minTimeThreshold) | ||
@@ -42,0 +19,0 @@ return []; |
{ | ||
"name": "ssb-conn-query", | ||
"description": "Module that helps querying potential SSB peer connections", | ||
"version": "0.4.6", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/staltz/ssb-conn-query", | ||
@@ -13,5 +13,5 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"ssb-conn-db": "~0.3.3", | ||
"ssb-conn-hub": "~0.2.7", | ||
"ssb-conn-staging": "~0.1.0" | ||
"ssb-conn-db": "~1.0.0", | ||
"ssb-conn-hub": "~1.0.0", | ||
"ssb-conn-staging": "~1.0.0" | ||
}, | ||
@@ -18,0 +18,0 @@ "devDependencies": { |
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"target": "es2018", | ||
"module": "commonjs", | ||
@@ -5,0 +5,0 @@ "lib": ["es5", "es2015", "es2016", "es2017"], |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
36106
21
975
+ Addedatomic-file-rw@0.3.0(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addedssb-conn-db@1.0.5(transitive)
+ Addedssb-conn-hub@1.0.0(transitive)
+ Addedssb-conn-staging@1.0.0(transitive)
+ Addedssb-ref@2.16.0(transitive)
- Removedatomic-file@2.1.1(transitive)
- Removeddebug@4.1.1(transitive)
- Removedflumecodec@0.0.1(transitive)
- Removedlevel-codec@6.2.0(transitive)
- Removedssb-conn-db@0.3.3(transitive)
- Removedssb-conn-hub@0.2.7(transitive)
- Removedssb-conn-staging@0.1.0(transitive)
Updatedssb-conn-db@~1.0.0
Updatedssb-conn-hub@~1.0.0
Updatedssb-conn-staging@~1.0.0