Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ssb-conn

Package Overview
Dependencies
Maintainers
1
Versions
92
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ssb-conn - npm Package Compare versions

Comparing version 0.11.2 to 0.11.3

37

lib/conn-scheduler.js

@@ -95,3 +95,4 @@ "use strict";

this.weBlockThem = ([_addr, data]) => {
if (!data || !data.key)
var _a;
if (!((_a = data) === null || _a === void 0 ? void 0 : _a.key))
return false;

@@ -101,3 +102,4 @@ return this.hops[data.key] === -1;

this.weFollowThem = ([_addr, data]) => {
if (!data || !data.key)
var _a;
if (!((_a = data) === null || _a === void 0 ? void 0 : _a.key))
return false;

@@ -133,4 +135,4 @@ const h = this.hops[data.key];

this.stop = () => {
if (this.ssb.lan && this.ssb.lan.stop)
this.ssb.lan.stop();
var _a, _b, _c;
(_c = (_a = this.ssb.lan) === null || _a === void 0 ? void 0 : (_b = _a).stop) === null || _c === void 0 ? void 0 : _c.call(_b);
this.ssb.conn.hub().reset();

@@ -149,6 +151,7 @@ this.closed = true;

this.ssb.post((msg) => {
var _a;
if (msg.value.author != this.ssb.id) {
this.lastMessageAt = Date.now();
}
if (msg.value.content && msg.value.content.type === 'contact') {
if (((_a = msg.value.content) === null || _a === void 0 ? void 0 : _a.type) === 'contact') {
this.loadHops(() => this.updateNow());

@@ -160,3 +163,4 @@ }

loadHops(doneCallback) {
if (!this.ssb.friends || !this.ssb.friends.hops) {
var _a;
if (!((_a = this.ssb.friends) === null || _a === void 0 ? void 0 : _a.hops)) {
debug('Warning: ssb-friends is missing, scheduling will miss some info');

@@ -350,2 +354,3 @@ return;

setupPubDiscovery() {
var _a, _b;
if (!this.hasSsbDb) {

@@ -355,5 +360,5 @@ debug('Warning: ssb-db is missing, scheduling will miss some info');

}
if (this.config.gossip && this.config.gossip.pub === false)
if (((_a = this.config.gossip) === null || _a === void 0 ? void 0 : _a.pub) === false)
return;
if (this.config.gossip && this.config.gossip.autoPopulate === false)
if (((_b = this.config.gossip) === null || _b === void 0 ? void 0 : _b.autoPopulate) === false)
return;

@@ -363,5 +368,3 @@ setTimeout(() => {

const pausable = Pausable();
pull(this.ssb.messagesByType({ type: 'pub', live: true, keys: false }), pull.filter((msg) => !msg.sync), pull.asyncMap((x, cb) => setTimeout(() => cb(null, x), 250)), pull.filter((msg) => msg.content &&
msg.content.address &&
Ref.isAddress(msg.content.address)), pausable, pull.drain((msg) => {
pull(this.ssb.messagesByType({ type: 'pub', live: true, keys: false }), pull.filter((msg) => !msg.sync), pull.asyncMap((x, cb) => setTimeout(() => cb(null, x), 250)), pull.filter((msg) => { var _a; return ((_a = msg.content) === null || _a === void 0 ? void 0 : _a.address) && Ref.isAddress(msg.content.address); }), pausable, pull.drain((msg) => {
try {

@@ -398,3 +401,4 @@ const address = Ref.toMultiServerAddress(msg.content.address);

setupBluetoothDiscovery() {
if (!this.ssb.bluetooth || !this.ssb.bluetooth.nearbyScuttlebuttDevices) {
var _a;
if (!((_a = this.ssb.bluetooth) === null || _a === void 0 ? void 0 : _a.nearbyScuttlebuttDevices)) {
debug('Warning: ssb-bluetooth is missing, scheduling will miss some info');

@@ -423,3 +427,4 @@ return;

setupLanDiscovery() {
if (!this.ssb.lan || !this.ssb.lan.start || !this.ssb.lan.discoveredPeers) {
var _a, _b;
if (!((_a = this.ssb.lan) === null || _a === void 0 ? void 0 : _a.start) || !((_b = this.ssb.lan) === null || _b === void 0 ? void 0 : _b.discoveredPeers)) {
debug('Warning: ssb-lan is missing, scheduling will miss some info');

@@ -429,8 +434,8 @@ return;

pull(this.ssb.lan.discoveredPeers(), pull.drain(({ address, verified }) => {
const peer = Ref.parseAddress(address);
if (!peer || !peer.key)
const key = Ref.getKeyFromAddress(address);
if (!key)
return;
const data = {
type: 'lan',
key: peer.key,
key,
verified,

@@ -437,0 +442,0 @@ };

@@ -41,10 +41,6 @@ "use strict";

const data = (typeof b === 'object' ? b : {});
this._hub
.connect(address, data)
.then(result => cb && cb(null, result), err => cb && cb(err));
this._hub.connect(address, data).then(result => { var _a; return (_a = cb) === null || _a === void 0 ? void 0 : _a(null, result); }, err => { var _a; return (_a = cb) === null || _a === void 0 ? void 0 : _a(err); });
};
this.disconnect = (address, cb) => {
this._hub
.disconnect(address)
.then(result => cb && cb(null, result), err => cb && cb(err));
this._hub.disconnect(address).then(result => { var _a; return (_a = cb) === null || _a === void 0 ? void 0 : _a(null, result); }, err => { var _a; return (_a = cb) === null || _a === void 0 ? void 0 : _a(err); });
};

@@ -74,6 +70,7 @@ this.peers = () => this._hub.liveEntries();

this.ping = () => {
var _a, _b;
const MIN = 10e3;
const DEFAULT = 5 * 60e3;
const MAX = 30 * 60e3;
let timeout = (this.config.timers && this.config.timers.ping) || DEFAULT;
let timeout = (_b = (_a = this.config.timers) === null || _a === void 0 ? void 0 : _a.ping, (_b !== null && _b !== void 0 ? _b : DEFAULT));
timeout = Math.max(MIN, Math.min(timeout, MAX));

@@ -122,3 +119,4 @@ return ping({ timeout });

maybeAutoStartScheduler() {
if (this.config.conn && this.config.conn.autostart === false) {
var _a;
if (((_a = this.config.conn) === null || _a === void 0 ? void 0 : _a.autostart) === false) {
}

@@ -125,0 +123,0 @@ else {

@@ -23,2 +23,3 @@ "use strict";

function toAddressString(address) {
var _a;
if (isPeerObject(address)) {

@@ -28,3 +29,3 @@ if (ref.isAddress(address.address))

let protocol = 'net';
if (address.host && address.host.endsWith('.onion'))
if ((_a = address.host) === null || _a === void 0 ? void 0 : _a.endsWith('.onion'))
protocol = 'onion';

@@ -63,2 +64,3 @@ return ([protocol, address.host, address.port].join(':') +

constructor(ssb, cfg) {
var _a;
this.peers = () => {

@@ -99,2 +101,3 @@ if (this.latestWarning + 10e3 < Date.now()) {

this.connect = (addr, cb) => {
var _a, _b;
console.error('DEPRECATED gossip.connect() was called. Use ssb-conn instead');

@@ -110,4 +113,4 @@ let addressString;

this.add(addressString, 'manual');
const stagedData = this.conn.staging().get(addressString) || {};
const dbData = this.conn.db().get(addressString) || {};
const stagedData = (_a = this.conn.staging().get(addressString), (_a !== null && _a !== void 0 ? _a : {}));
const dbData = (_b = this.conn.db().get(addressString), (_b !== null && _b !== void 0 ? _b : {}));
const data = Object.assign(Object.assign({}, dbData), stagedData);

@@ -133,2 +136,3 @@ this.conn.connect(addressString, data, cb);

this.add = (addr, source) => {
var _a;
console.error('DEPRECATED gossip.add() was called. Use ssb-conn instead');

@@ -156,6 +160,6 @@ const [addressString, parsed] = validateAddr(addr);

type: 'discover',
peer: Object.assign(Object.assign({}, parsed), { state: this.conn.hub().getState(addressString), source: source || 'manual' }),
source: source || 'manual',
peer: Object.assign(Object.assign({}, parsed), { state: this.conn.hub().getState(addressString), source: (source !== null && source !== void 0 ? source : 'manual') }),
source: (source !== null && source !== void 0 ? source : 'manual'),
});
return this.conn.db().get(addressString) || parsed;
return _a = this.conn.db().get(addressString), (_a !== null && _a !== void 0 ? _a : parsed);
}

@@ -190,3 +194,3 @@ };

this.setupConnectionListeners();
if (cfg.conn && cfg.conn.autostart === false) {
if (((_a = cfg.conn) === null || _a === void 0 ? void 0 : _a.autostart) === false) {
}

@@ -193,0 +197,0 @@ else {

@@ -12,6 +12,10 @@ "use strict";

pull(pp, rpc.gossip.ping({ timeout: PING_TIMEOUT }, (err) => {
if (err && err.name === 'TypeError') {
db.update(address, (prev) => ({
ping: Object.assign(Object.assign({}, (prev.ping || {})), { fail: true }),
}));
var _a;
if (((_a = err) === null || _a === void 0 ? void 0 : _a.name) === 'TypeError') {
db.update(address, (prev) => {
var _a;
return ({
ping: Object.assign(Object.assign({}, (_a = prev.ping, (_a !== null && _a !== void 0 ? _a : {}))), { fail: true }),
});
});
}

@@ -33,7 +37,10 @@ }), pp);

function onConnectingFailed(ev) {
db.update(ev.address, (prev) => ({
failure: (prev.failure || 0) + 1,
stateChange: Date.now(),
duration: stats(prev.duration, 0),
}));
db.update(ev.address, (prev) => {
var _a;
return ({
failure: (_a = prev.failure, (_a !== null && _a !== void 0 ? _a : 0)) + 1,
stateChange: Date.now(),
duration: stats(prev.duration, 0),
});
});
}

@@ -40,0 +47,0 @@ function onConnected(ev) {

{
"name": "ssb-conn",
"description": "SSB plugin for establishing and managing peer connections",
"version": "0.11.2",
"version": "0.11.3",
"homepage": "https://github.com/staltz/ssb-conn",

@@ -43,3 +43,3 @@ "main": "lib/index.js",

"tape": "^4.9.2",
"typescript": "3.6.x"
"typescript": "3.7.x"
},

@@ -46,0 +46,0 @@ "scripts": {

@@ -54,9 +54,9 @@ <div style="text-align:center" align="center">

| **`forget(addr)`** | `sync` | Removes (from cold storage) connection information about a peer known by its multiserver address `addr`. |
| **`dbPeers()`** | `sync` | Returns an Iterable of (cold stored) database "entries" (see definition below) known at the moment. Does not reactively update once the database is written to. |
| **`dbPeers()`** | `sync` | Returns an Iterable of ConnDB entries known at the moment. Does not reactively update once the database is written to. |
| **`connect(addr, data?)`** | `async` | Connects to a peer known by its multiserver address `addr`, and stores additional optional `data` (as an object) during its connection lifespan. |
| **`disconnect(addr)`** | `async` | Disconnects a peer known by its multiserver address `addr`. |
| **`peers()`** | `source` | A pull-stream that emits an array of all connection "entries" (see definition below) whenever any connection updates (changes it state: connecting, disconnecting, connected, etc). |
| **`peers()`** | `source` | A pull-stream that emits an array of all ConnHub entries whenever any connection updates (i.e. changes it state: connecting, disconnecting, connected, etc). |
| **`stage(addr, data?)`** | `sync` | Registers a suggested connection to a new peer, known by its multiserver address `addr` and additional optional `data` (as an object). |
| **`unstage(addr)`** | `sync` | Unregisters a suggested connection the peer known by its multiserver address `addr`. |
| **`stagedPeers()`** | `source` | A pull-stream that emits an array of all staged "entries" (see definition below) whenever any staging status updates (upon stage() or unstage()). |
| **`stagedPeers()`** | `source` | A pull-stream that emits an array of all ConnStaging entries whenever any staging status updates (upon stage() or unstage()). |
| **`start()`** | `sync` | Triggers the start of the connection scheduler in CONN. |

@@ -70,3 +70,3 @@ | **`stop()`** | `sync` | Stops the CONN scheduler if it is currently active. |

An "entry" is a (tuple) array with a multiserver address (string that [follows some rules](https://github.com/dominictarr/multiserver-address)) and data (an object):
An "entry" is a (tuple) array of form:

@@ -76,3 +76,42 @@ ```javascript

```
where:
- `addr` is a multiserver address (a **string** that [follows some rules](https://github.com/dominictarr/multiserver-address))
- `data` is an **object** with additional information about the peer (fields marked 🔷 are important and often used, fields marked 🔹 come from CONN, fields marked 🔸 are ad-hoc and added by various other modules, and fields suffixed with `?` are not always present):
🔷 `key: string`: the peer's public key / feedId
🔷 `state?: 'connecting' | 'connected' | 'disconnecting'`: (only from `peers()`) the peer's current connection status
🔷 `type?: string`: what type of peer this is; it can be any string, but often is either `'lan'`, `'bt'`, `'pub'`, `'room'`, `'room-endpoint'`, `'dht'`
🔹 `inferredType?: 'bt' | 'lan' | 'dht' | 'internet' | 'tunnel'`: (only from `peers()`) when there is no `type` field, e.g. when a new and unknown peer initiates a client connection with us (as a server), then ConnHub makes a guess what type it is
🔹 `birth?: number`: Unix timestamp for when this peer was added to ConnDB
🔹 `stateChange?: number`: Unix timestamp for the last time the field `state` was changed; this is stored in ConnDB
🔹 `hubBirth?: number`: Unix timestamp for when this peer was added to ConnHub
🔹 `hubUpdated?: number`: Unix timestamp for when this data object was last updated in ConnHub
🔹 `stagingBirth?: number`: Unix timestamp for when this peer was added to ConnStaging
🔹 `stagingUpdated?: number`: Unix timestamp for when this data object was last updated in ConnStaging
🔹 `autoconnect?: boolean`: indicates whether this peer should be considered for connection in the scheduler
🔹 `failure?: number`: typically in ConnDB, this is the number of connection errors since the last successful connection
🔹 `duration?: object`: typically in ConnDB, this is a [statistics](https://www.npmjs.com/package/statistics) object to measure the duration of connection with this peer
🔹 `ping?: object`: typically in ConnDB, this is [statistics](https://www.npmjs.com/package/statistics) object of various ping health measurements
🔹 `pool?: 'db' | 'hub' | 'staging'`: this only appears in ConnQuery APIs, and indicates from which pool (ConnDB or ConnHub or ConnStaging) was this peer picked
🔸 `name?: string`: a nickname for this peer, when there isn't an [ssb-about](https://github.com/ssbc/ssb-about) name
🔸 `room?: string`: (only if `type = 'room-endpoint'`) the public key of the [room](https://github.com/staltz/ssb-room) server where this peer is in
🔸 `onlineCount?: number`: (only if `type = 'room'`) the number of room endpoints currently connected to this room
## Gossip compatibility

@@ -79,0 +118,0 @@

@@ -131,3 +131,3 @@ import ConnQuery = require('ssb-conn-query');

}
if (msg.value.content && msg.value.content.type === 'contact') {
if (msg.value.content?.type === 'contact') {
this.loadHops(() => this.updateNow());

@@ -140,3 +140,3 @@ }

private loadHops(doneCallback?: () => void) {
if (!this.ssb.friends || !this.ssb.friends.hops) {
if (!this.ssb.friends?.hops) {
debug('Warning: ssb-friends is missing, scheduling will miss some info');

@@ -171,3 +171,3 @@ return;

private weBlockThem = ([_addr, data]: [string, {key?: string}]) => {
if (!data || !data.key) return false;
if (!data?.key) return false;
return this.hops[data.key] === -1;

@@ -177,3 +177,3 @@ };

private weFollowThem = ([_addr, data]: [string, {key?: string}]) => {
if (!data || !data.key) return false;
if (!data?.key) return false;
const h = this.hops[data.key];

@@ -392,4 +392,4 @@ return h > 0 && h <= 1;

if (this.config.gossip && this.config.gossip.pub === false) return;
if (this.config.gossip && this.config.gossip.autoPopulate === false) return;
if (this.config.gossip?.pub === false) return;
if (this.config.gossip?.autoPopulate === false) return;

@@ -408,5 +408,3 @@ setTimeout(() => {

(msg: Msg<PubContent>['value']) =>
msg.content &&
msg.content.address &&
Ref.isAddress(msg.content.address),
msg.content?.address && Ref.isAddress(msg.content.address),
),

@@ -450,3 +448,3 @@ pausable,

private setupBluetoothDiscovery() {
if (!this.ssb.bluetooth || !this.ssb.bluetooth.nearbyScuttlebuttDevices) {
if (!this.ssb.bluetooth?.nearbyScuttlebuttDevices) {
debug(

@@ -482,3 +480,3 @@ 'Warning: ssb-bluetooth is missing, scheduling will miss some info',

private setupLanDiscovery() {
if (!this.ssb.lan || !this.ssb.lan.start || !this.ssb.lan.discoveredPeers) {
if (!this.ssb.lan?.start || !this.ssb.lan?.discoveredPeers) {
debug('Warning: ssb-lan is missing, scheduling will miss some info');

@@ -491,7 +489,7 @@ return;

pull.drain(({address, verified}: LANDiscovery) => {
const peer = Ref.parseAddress(address);
if (!peer || !peer.key) return;
const key: FeedId | undefined = Ref.getKeyFromAddress(address);
if (!key) return;
const data: Partial<StagedData> = {
type: 'lan',
key: peer.key,
key,
verified,

@@ -561,3 +559,3 @@ };

public stop = () => {
if (this.ssb.lan && this.ssb.lan.stop) this.ssb.lan.stop();
this.ssb.lan?.stop?.();
this.ssb.conn.hub().reset();

@@ -564,0 +562,0 @@ this.closed = true;

@@ -52,3 +52,3 @@ import ConnDB = require('ssb-conn-db');

private maybeAutoStartScheduler() {
if (this.config.conn && this.config.conn.autostart === false) {
if (this.config.conn?.autostart === false) {
// opt-out from starting the scheduler

@@ -119,5 +119,6 @@ } else {

this._hub
.connect(address, data)
.then(result => cb && cb(null, result), err => cb && cb(err));
this._hub.connect(address, data).then(
result => cb?.(null, result),
err => cb?.(err),
);
};

@@ -127,5 +128,6 @@

public disconnect = (address: string, cb?: Callback<any>) => {
this._hub
.disconnect(address)
.then(result => cb && cb(null, result), err => cb && cb(err));
this._hub.disconnect(address).then(
result => cb?.(null, result),
err => cb?.(err),
);
};

@@ -174,3 +176,3 @@

const MAX = 30 * 60e3;
let timeout = (this.config.timers && this.config.timers.ping) || DEFAULT;
let timeout = this.config.timers?.ping ?? DEFAULT;
timeout = Math.max(MIN, Math.min(timeout, MAX));

@@ -177,0 +179,0 @@ return ping({timeout});

@@ -22,3 +22,3 @@ import {ListenEvent as HubEvent} from 'ssb-conn-hub/lib/types';

let protocol = 'net';
if (address.host && address.host.endsWith('.onion')) protocol = 'onion';
if (address.host?.endsWith('.onion')) protocol = 'onion';
return (

@@ -76,3 +76,3 @@ [protocol, address.host, address.port].join(':') +

this.setupConnectionListeners();
if (cfg.conn && cfg.conn.autostart === false) {
if (cfg.conn?.autostart === false) {
// opt-out from starting the scheduler

@@ -206,4 +206,4 @@ } else {

this.add(addressString, 'manual');
const stagedData = this.conn.staging().get(addressString) || {};
const dbData = this.conn.db().get(addressString) || {};
const stagedData = this.conn.staging().get(addressString) ?? {};
const dbData = this.conn.db().get(addressString) ?? {};
const data = {...dbData, ...stagedData};

@@ -267,7 +267,7 @@

state: this.conn.hub().getState(addressString),
source: source || 'manual',
source: source ?? 'manual',
},
source: source || 'manual',
source: source ?? 'manual',
});
return this.conn.db().get(addressString) || parsed;
return this.conn.db().get(addressString) ?? parsed;
}

@@ -274,0 +274,0 @@ };

@@ -17,5 +17,5 @@ import ConnDB = require('ssb-conn-db');

rpc.gossip.ping({timeout: PING_TIMEOUT}, (err: any) => {
if (err && err.name === 'TypeError') {
if (err?.name === 'TypeError') {
db.update(address, (prev: any) => ({
ping: {...(prev.ping || {}), fail: true},
ping: {...(prev.ping ?? {}), fail: true},
}));

@@ -42,3 +42,3 @@ }

db.update(ev.address, (prev: any) => ({
failure: (prev.failure || 0) + 1,
failure: (prev.failure ?? 0) + 1,
stateChange: Date.now(),

@@ -45,0 +45,0 @@ duration: stats(prev.duration, 0),

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc